blob: aefc808391ac35eebafedef81686f047dfa98037 [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
wdenk29e7f5a2004-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 Majewski21185922015-08-24 00:21:43 +020014#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000015#include "bootp.h"
wdenk29e7f5a2004-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 Phillips576175b2012-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 Hershberger8f4b1352012-05-15 08:59:06 +000024#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000025
Stephen Warren69e1e352014-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 */
wdenk29e7f5a2004-03-12 00:14:09 +000036#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger8f4b1352012-05-15 08:59:06 +000037# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000038#else
wdenk29e7f5a2004-03-12 00:14:09 +000039# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000040#endif
Stephen Warren69e1e352014-07-25 17:30:48 -060041#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000042
Joe Hershberger8f4b1352012-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 Hershberger8f4b1352012-05-15 08:59:06 +000046#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk29e7f5a2004-03-12 00:14:09 +000047#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000048#endif
49
Thierry Reding8977cda2014-08-19 10:21:24 +020050#ifndef CONFIG_BOOTP_ID_CACHE_SIZE
51#define CONFIG_BOOTP_ID_CACHE_SIZE 4
52#endif
53
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -050054u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
Thierry Reding8977cda2014-08-19 10:21:24 +020055unsigned int bootp_num_ids;
Joe Hershberger2fe81a52015-04-08 01:41:09 -050056int bootp_try;
Stephen Warren69e1e352014-07-25 17:30:48 -060057ulong bootp_start;
58ulong bootp_timeout;
Joe Hershberger6d236432015-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
Alexandre Messier15971322016-02-01 17:08:57 -050063static ulong time_taken_max;
64
Jon Loeliger54f35c22007-07-09 17:45:14 -050065#if defined(CONFIG_CMD_DHCP)
Kim Phillips40c2c032012-10-29 13:34:33 +000066static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -050067static u32 dhcp_leasetime;
Joe Hershberger5874dec2015-04-08 01:41:01 -050068static struct in_addr dhcp_server_ip;
Stefan Brüns345cb832015-09-04 00:31:48 +020069static u8 dhcp_option_overload;
70#define OVERLOAD_FILE 1
71#define OVERLOAD_SNAME 2
Joe Hershberger5874dec2015-04-08 01:41:01 -050072static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
73 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000074
75/* For Debug */
wdenkb02744a2003-04-05 00:53:31 +000076#if 0
77static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000078{
79 switch (type) {
wdenk29e7f5a2004-03-12 00:14:09 +000080 case 1: return "DHCPDISCOVER"; break;
81 case 2: return "DHCPOFFER"; break;
82 case 3: return "DHCPREQUEST"; break;
83 case 4: return "DHCPDECLINE"; break;
84 case 5: return "DHCPACK"; break;
85 case 6: return "DHCPNACK"; break;
86 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000087 default: return "UNKNOWN/INVALID MSG TYPE"; break;
88 }
89}
wdenkb02744a2003-04-05 00:53:31 +000090#endif
Jon Loeligera9807e52007-07-10 11:05:02 -050091#endif
wdenk3861aa52002-09-27 23:19:37 +000092
Thierry Reding8977cda2014-08-19 10:21:24 +020093static void bootp_add_id(ulong id)
94{
95 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
96 size_t size = sizeof(bootp_ids) - sizeof(id);
97
98 memmove(bootp_ids, &bootp_ids[1], size);
99 bootp_ids[bootp_num_ids - 1] = id;
100 } else {
101 bootp_ids[bootp_num_ids] = id;
102 bootp_num_ids++;
103 }
104}
105
106static bool bootp_match_id(ulong id)
107{
108 unsigned int i;
109
110 for (i = 0; i < bootp_num_ids; i++)
111 if (bootp_ids[i] == id)
112 return true;
113
114 return false;
115}
116
Stefan Brünsa48cd932015-08-27 23:53:26 +0200117static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
118 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000119{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500120 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000121 int retval = 0;
122
123 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
124 retval = -1;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500125 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000126 retval = -2;
Stefan Brünsa48cd932015-08-27 23:53:26 +0200127 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000128 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000129 else if (bp->bp_htype != HWT_ETHER)
130 retval = -4;
131 else if (bp->bp_hlen != HWL_ETHER)
132 retval = -5;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500133 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000134 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +0000135
Robin Getz9e0a4d62009-07-23 03:01:03 -0400136 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000137
138 return retval;
139}
140
141/*
142 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
143 */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500144static void store_net_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000145{
Wilson Callan22bcd6e2007-07-28 10:56:13 -0400146#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500147 struct in_addr tmp_ip;
Joe Hershberger9d37a582012-05-23 07:59:18 +0000148
Joe Hershberger5874dec2015-04-08 01:41:01 -0500149 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
150 if (tmp_ip.s_addr != 0)
151 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500152 memcpy(net_server_ethaddr,
153 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
Stefan Brüns345cb832015-09-04 00:31:48 +0200154 if (
155#if defined(CONFIG_CMD_DHCP)
156 !(dhcp_option_overload & OVERLOAD_FILE) &&
157#endif
158 (strlen(bp->bp_file) > 0)) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500159 copy_filename(net_boot_file_name, bp->bp_file,
160 sizeof(net_boot_file_name));
Stefan Brüns345cb832015-09-04 00:31:48 +0200161 }
wdenk3861aa52002-09-27 23:19:37 +0000162
Joe Hershberger290c8992015-04-08 01:41:02 -0500163 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000164
165 /* Propagate to environment:
wdenk57b2d802003-06-27 21:31:46 +0000166 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000167 * not contain a new value
168 */
Joe Hershberger290c8992015-04-08 01:41:02 -0500169 if (*net_boot_file_name)
170 setenv("bootfile", net_boot_file_name);
Wu, Josha9c2cbd2014-11-18 13:07:08 +0800171#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500172 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000173}
174
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000175static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000176{
177 if (curlen >= maxlen) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000178 printf("*** WARNING: %s is too long (%d - max: %d)"
179 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000180 curlen = maxlen - 1;
181 }
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000182 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000183}
184
Jon Loeliger54f35c22007-07-09 17:45:14 -0500185#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000186
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500187static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000188{
wdenk29e7f5a2004-03-12 00:14:09 +0000189 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000190
Robin Getz9e0a4d62009-07-23 03:01:03 -0400191 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500192 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000193
Joe Hershberger290c8992015-04-08 01:41:02 -0500194 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000195
wdenk29e7f5a2004-03-12 00:14:09 +0000196 switch (*ext) {
197 /* Fixed length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000198 case 1: /* Subnet mask */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500199 if (net_netmask.s_addr == 0)
200 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000201 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000202 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000203 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000204 /* Variable length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000205 case 3: /* Gateways list */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500206 if (net_gateway.s_addr == 0)
207 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000208 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000209 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000210 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000211 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000212 break;
213 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500214 if (net_dns_server.s_addr == 0)
215 net_copy_ip(&net_dns_server,
216 (struct in_addr *)(ext + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500217#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500218 if ((net_dns_server2.s_addr == 0) && (size > 4))
219 net_copy_ip(&net_dns_server2,
220 (struct in_addr *)(ext + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000221#endif
wdenk3861aa52002-09-27 23:19:37 +0000222 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000223 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000224 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000225 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000226 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000227 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000228 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000229 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000230 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000231 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000232 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000233 case 12: /* Host name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500234 if (net_hostname[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000235 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500236 sizeof(net_hostname), size);
237 memcpy(&net_hostname, ext + 2, size);
238 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000239 }
240 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000241 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000242 if (size == 2)
Joe Hershberger290c8992015-04-08 01:41:02 -0500243 net_boot_file_expected_size_in_blocks =
244 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000245 else if (size == 4)
Joe Hershberger290c8992015-04-08 01:41:02 -0500246 net_boot_file_expected_size_in_blocks =
247 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000248 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000249 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000250 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000251 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000252 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000253 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000254 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000255 case 17: /* Root path */
Joe Hershberger6d236432015-04-08 01:41:03 -0500256 if (net_root_path[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000257 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500258 sizeof(net_root_path), size);
259 memcpy(&net_root_path, ext + 2, size);
260 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000261 }
262 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000263 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000264 /*
wdenk57b2d802003-06-27 21:31:46 +0000265 * This can be used to send the information of the
266 * vendor area in another file that the client can
267 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000268 */
269 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000270 /* IP host layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000271 case 40: /* NIS Domain name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500272 if (net_nis_domain[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000273 size = truncate_sz("NIS Domain Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500274 sizeof(net_nis_domain), size);
275 memcpy(&net_nis_domain, ext + 2, size);
276 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000277 }
278 break;
Luuk Paulussen6380e012011-05-16 18:29:19 +0000279#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
280 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500281 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen6380e012011-05-16 18:29:19 +0000282 break;
283#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000284 /* Application layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000285 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000286 /*
wdenk57b2d802003-06-27 21:31:46 +0000287 * Binary information to exchange specific
288 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000289 */
290 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000291 /* Reserved (custom) fields (128..254) */
292 }
wdenk3861aa52002-09-27 23:19:37 +0000293}
294
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500295static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000296{
wdenk29e7f5a2004-03-12 00:14:09 +0000297 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000298
Robin Getz9e0a4d62009-07-23 03:01:03 -0400299 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000300
wdenk29e7f5a2004-03-12 00:14:09 +0000301 while ((ext < end) && (*ext != 0xff)) {
302 if (*ext == 0) {
303 ext++;
304 } else {
305 u8 *opt = ext;
306
307 ext += ext[1] + 2;
308 if (ext <= end)
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500309 bootp_process_vendor_field(opt);
wdenk29e7f5a2004-03-12 00:14:09 +0000310 }
wdenk3861aa52002-09-27 23:19:37 +0000311 }
wdenk3861aa52002-09-27 23:19:37 +0000312
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000313 debug("[BOOTP] Received fields:\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500314 if (net_netmask.s_addr)
315 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000316
Joe Hershberger5874dec2015-04-08 01:41:01 -0500317 if (net_gateway.s_addr)
318 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000319
Joe Hershberger290c8992015-04-08 01:41:02 -0500320 if (net_boot_file_expected_size_in_blocks)
321 debug("net_boot_file_expected_size_in_blocks : %d\n",
322 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000323
Joe Hershberger6d236432015-04-08 01:41:03 -0500324 if (net_hostname[0])
325 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000326
Joe Hershberger6d236432015-04-08 01:41:03 -0500327 if (net_root_path[0])
328 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000329
Joe Hershberger6d236432015-04-08 01:41:03 -0500330 if (net_nis_domain[0])
331 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000332
Luuk Paulussen6380e012011-05-16 18:29:19 +0000333#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500334 if (net_ntp_server)
335 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen6380e012011-05-16 18:29:19 +0000336#endif
wdenk3861aa52002-09-27 23:19:37 +0000337}
Simon Glassab068eb2011-06-13 16:13:12 -0700338
wdenk3861aa52002-09-27 23:19:37 +0000339/*
340 * Handle a BOOTP received packet.
341 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500342static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
343 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000344{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500345 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000346
Robin Getz9e0a4d62009-07-23 03:01:03 -0400347 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500348 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000349
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500350 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000351
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000352 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +0200353 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000354 return;
355
356 /*
wdenk29e7f5a2004-03-12 00:14:09 +0000357 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000358 */
Thomas Chou3948aa02015-08-25 20:54:24 +0800359#if defined(CONFIG_STATUS_LED) && defined(STATUS_LED_BOOT)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000360 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000361#endif
362
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500363 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000364
365 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500366 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500367 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000368
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500369 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +0000370 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000371
Robin Getz9e0a4d62009-07-23 03:01:03 -0400372 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000373
Simon Glass5234ad12011-10-27 06:24:32 +0000374 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000375}
Jon Loeligera9807e52007-07-10 11:05:02 -0500376#endif
wdenk3861aa52002-09-27 23:19:37 +0000377
378/*
379 * Timeout on BOOTP/DHCP request.
380 */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500381static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000382{
Stephen Warren69e1e352014-07-25 17:30:48 -0600383 ulong time_taken = get_timer(bootp_start);
384
Alexandre Messier15971322016-02-01 17:08:57 -0500385 if (time_taken >= time_taken_max) {
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000386#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warren69e1e352014-07-25 17:30:48 -0600387 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advans7232ee52012-07-19 01:23:21 +0000388 net_set_state(NETLOOP_FAIL);
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000389#else
Stephen Warren69e1e352014-07-25 17:30:48 -0600390 puts("\nRetry time exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500391 net_start_again();
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000392#endif
wdenk3861aa52002-09-27 23:19:37 +0000393 } else {
Stephen Warren69e1e352014-07-25 17:30:48 -0600394 bootp_timeout *= 2;
Thierry Reding8977cda2014-08-19 10:21:24 +0200395 if (bootp_timeout > 2000)
396 bootp_timeout = 2000;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500397 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500398 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000399 }
400}
401
Ilya Yanok30876582012-09-17 10:26:25 +0000402#define put_vci(e, str) \
403 do { \
404 size_t vci_strlen = strlen(str); \
405 *e++ = 60; /* Vendor Class Identifier */ \
406 *e++ = vci_strlen; \
407 memcpy(e, str, vci_strlen); \
408 e += vci_strlen; \
409 } while (0)
410
wdenk3861aa52002-09-27 23:19:37 +0000411/*
412 * Initialize BOOTP extension fields in the request.
413 */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500414#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500415static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
416 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000417{
wdenk29e7f5a2004-03-12 00:14:09 +0000418 u8 *start = e;
419 u8 *cnt;
Jason Hobbsea3202812011-08-31 05:37:31 +0000420#if defined(CONFIG_BOOTP_PXE)
421 char *uuid;
Jason Hobbsea3202812011-08-31 05:37:31 +0000422 u16 clientarch;
423#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000424
Jon Loeliger5336a762007-07-09 22:08:34 -0500425#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000426 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000427#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500428#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200429 char *hostname;
stroesee0aadfb2003-08-28 14:17:32 +0000430#endif
wdenk3861aa52002-09-27 23:19:37 +0000431
wdenk29e7f5a2004-03-12 00:14:09 +0000432 *e++ = 99; /* RFC1048 Magic Cookie */
433 *e++ = 130;
434 *e++ = 83;
435 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000436
wdenk29e7f5a2004-03-12 00:14:09 +0000437 *e++ = 53; /* DHCP Message Type */
438 *e++ = 1;
439 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000440
wdenk29e7f5a2004-03-12 00:14:09 +0000441 *e++ = 57; /* Maximum DHCP Message Size */
442 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000443 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
444 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000445
Joe Hershberger5874dec2015-04-08 01:41:01 -0500446 if (server_ip.s_addr) {
447 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000448
wdenk29e7f5a2004-03-12 00:14:09 +0000449 *e++ = 54; /* ServerID */
450 *e++ = 4;
451 *e++ = tmp >> 24;
452 *e++ = tmp >> 16;
453 *e++ = tmp >> 8;
454 *e++ = tmp & 0xff;
455 }
wdenk3861aa52002-09-27 23:19:37 +0000456
Joe Hershberger5874dec2015-04-08 01:41:01 -0500457 if (requested_ip.s_addr) {
458 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000459
wdenk29e7f5a2004-03-12 00:14:09 +0000460 *e++ = 50; /* Requested IP */
461 *e++ = 4;
462 *e++ = tmp >> 24;
463 *e++ = tmp >> 16;
464 *e++ = tmp >> 8;
465 *e++ = tmp & 0xff;
466 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000468 hostname = getenv("hostname");
469 if (hostname) {
470 int hostnamelen = strlen(hostname);
wdenk29e7f5a2004-03-12 00:14:09 +0000471
472 *e++ = 12; /* Hostname */
473 *e++ = hostnamelen;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000474 memcpy(e, hostname, hostnamelen);
wdenk29e7f5a2004-03-12 00:14:09 +0000475 e += hostnamelen;
476 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000477#endif
478
479#if defined(CONFIG_BOOTP_PXE)
480 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
481 *e++ = 93; /* Client System Architecture */
482 *e++ = 2;
483 *e++ = (clientarch >> 8) & 0xff;
484 *e++ = clientarch & 0xff;
485
486 *e++ = 94; /* Client Network Interface Identifier */
487 *e++ = 3;
488 *e++ = 1; /* type field for UNDI */
489 *e++ = 0; /* major revision */
490 *e++ = 0; /* minor revision */
491
492 uuid = getenv("pxeuuid");
493
494 if (uuid) {
495 if (uuid_str_valid(uuid)) {
496 *e++ = 97; /* Client Machine Identifier */
497 *e++ = 17;
498 *e++ = 0; /* type 0 - UUID */
499
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200500 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsea3202812011-08-31 05:37:31 +0000501 e += 16;
502 } else {
503 printf("Invalid pxeuuid: %s\n", uuid);
504 }
505 }
Ilya Yanok30876582012-09-17 10:26:25 +0000506#endif
Jason Hobbsea3202812011-08-31 05:37:31 +0000507
Hannes Petermaier1ea88522015-08-25 12:17:59 +0200508#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
509 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
510#elif defined(CONFIG_BOOTP_VCI_STRING)
Ilya Yanok30876582012-09-17 10:26:25 +0000511 put_vci(e, CONFIG_BOOTP_VCI_STRING);
stroesee0aadfb2003-08-28 14:17:32 +0000512#endif
513
Jon Loeliger5336a762007-07-09 22:08:34 -0500514#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000515 x = dhcp_vendorex_prep(e);
516 if (x)
wdenk29e7f5a2004-03-12 00:14:09 +0000517 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000518#endif
519
wdenk29e7f5a2004-03-12 00:14:09 +0000520 *e++ = 55; /* Parameter Request List */
521 cnt = e++; /* Pointer to count of requested items */
522 *cnt = 0;
Jon Loeliger5336a762007-07-09 22:08:34 -0500523#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000524 *e++ = 1; /* Subnet Mask */
525 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000526#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500527#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000528 *e++ = 2;
529 *cnt += 1;
530#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500531#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000532 *e++ = 3; /* Router Option */
533 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000534#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500535#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000536 *e++ = 6; /* DNS Server(s) */
537 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000538#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500539#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000540 *e++ = 12; /* Hostname */
541 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000542#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500543#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000544 *e++ = 13; /* Boot File Size */
545 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000546#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500547#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000548 *e++ = 17; /* Boot path */
549 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000550#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500551#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000552 *e++ = 40; /* NIS Domain name request */
553 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000554#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500555#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000556 *e++ = 42;
557 *cnt += 1;
558#endif
Jason Liu1c7dca52010-11-14 12:23:09 +0800559 /* no options, so back up to avoid sending an empty request list */
560 if (*cnt == 0)
561 e -= 2;
562
wdenk29e7f5a2004-03-12 00:14:09 +0000563 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000564
wdenk29e7f5a2004-03-12 00:14:09 +0000565 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000566#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glassa0978eb2011-02-02 15:03:28 -0800567 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk29e7f5a2004-03-12 00:14:09 +0000568 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000569#endif
570
wdenk29e7f5a2004-03-12 00:14:09 +0000571 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000572}
573
Jon Loeligera9807e52007-07-10 11:05:02 -0500574#else
wdenk3861aa52002-09-27 23:19:37 +0000575/*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000576 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000577 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500578static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000579{
wdenk29e7f5a2004-03-12 00:14:09 +0000580 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000581
wdenk29e7f5a2004-03-12 00:14:09 +0000582 *e++ = 99; /* RFC1048 Magic Cookie */
583 *e++ = 130;
584 *e++ = 83;
585 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000586
Jon Loeliger54f35c22007-07-09 17:45:14 -0500587#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000588 *e++ = 53; /* DHCP Message Type */
589 *e++ = 1;
590 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000591
wdenk29e7f5a2004-03-12 00:14:09 +0000592 *e++ = 57; /* Maximum DHCP Message Size */
593 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000594 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
595 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeligera9807e52007-07-10 11:05:02 -0500596#endif
wdenk3861aa52002-09-27 23:19:37 +0000597
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000598#if defined(CONFIG_BOOTP_VCI_STRING) || \
599 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
600#ifdef CONFIG_SPL_BUILD
601 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
602#else
Ilya Yanok30876582012-09-17 10:26:25 +0000603 put_vci(e, CONFIG_BOOTP_VCI_STRING);
604#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000605#endif
Ilya Yanok30876582012-09-17 10:26:25 +0000606
Jon Loeliger5336a762007-07-09 22:08:34 -0500607#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000608 *e++ = 1; /* Subnet mask request */
609 *e++ = 4;
610 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000611#endif
612
Jon Loeliger5336a762007-07-09 22:08:34 -0500613#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000614 *e++ = 3; /* Default gateway request */
615 *e++ = 4;
616 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000617#endif
618
Jon Loeliger5336a762007-07-09 22:08:34 -0500619#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000620 *e++ = 6; /* Domain Name Server */
621 *e++ = 4;
622 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000623#endif
624
Jon Loeliger5336a762007-07-09 22:08:34 -0500625#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000626 *e++ = 12; /* Host name request */
627 *e++ = 32;
628 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000629#endif
630
Jon Loeliger5336a762007-07-09 22:08:34 -0500631#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000632 *e++ = 13; /* Boot file size */
633 *e++ = 2;
634 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000635#endif
636
Jon Loeliger5336a762007-07-09 22:08:34 -0500637#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000638 *e++ = 17; /* Boot path */
639 *e++ = 32;
640 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000641#endif
642
Jon Loeliger5336a762007-07-09 22:08:34 -0500643#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000644 *e++ = 40; /* NIS Domain name request */
645 *e++ = 32;
646 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000647#endif
Luuk Paulussen6380e012011-05-16 18:29:19 +0000648#if defined(CONFIG_BOOTP_NTPSERVER)
649 *e++ = 42;
650 *e++ = 4;
651 e += 4;
652#endif
wdenk3861aa52002-09-27 23:19:37 +0000653
wdenk29e7f5a2004-03-12 00:14:09 +0000654 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000655
wdenk29e7f5a2004-03-12 00:14:09 +0000656 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000657}
Jon Loeligera9807e52007-07-10 11:05:02 -0500658#endif
wdenk3861aa52002-09-27 23:19:37 +0000659
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500660void bootp_reset(void)
Stephen Warren69e1e352014-07-25 17:30:48 -0600661{
Thierry Reding8977cda2014-08-19 10:21:24 +0200662 bootp_num_ids = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500663 bootp_try = 0;
Stephen Warren69e1e352014-07-25 17:30:48 -0600664 bootp_start = get_timer(0);
Thierry Reding8977cda2014-08-19 10:21:24 +0200665 bootp_timeout = 250;
Stephen Warren69e1e352014-07-25 17:30:48 -0600666}
667
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500668void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000669{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000670 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500671 struct bootp_hdr *bp;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000672 int extlen, pktlen, iplen;
673 int eth_hdr_size;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000674#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek764c29d2014-07-11 11:39:37 +0200675 ulong rand_ms;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000676#endif
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500677 u32 bootp_id;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500678 struct in_addr zero_ip;
679 struct in_addr bcast_ip;
Alexandre Messier15971322016-02-01 17:08:57 -0500680 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000681
Simon Glass768cbf02011-12-10 11:08:06 +0000682 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger54f35c22007-07-09 17:45:14 -0500683#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000684 dhcp_state = INIT;
685#endif
686
Alexandre Messier15971322016-02-01 17:08:57 -0500687 ep = getenv("bootpretryperiod");
688 if (ep != NULL)
689 time_taken_max = simple_strtoul(ep, NULL, 10);
690 else
691 time_taken_max = TIMEOUT_MS;
692
wdenk3861aa52002-09-27 23:19:37 +0000693#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500694 if (bootp_try == 0)
Joe Hershberger797f2c52012-05-23 07:57:58 +0000695 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000696
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500697 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
698 rand_ms = rand() >> (22 - bootp_try);
Joe Hershberger797f2c52012-05-23 07:57:58 +0000699 else /* After 3rd BOOTP request max 8192 * 1ms */
700 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000701
Joe Hershberger797f2c52012-05-23 07:57:58 +0000702 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek764c29d2014-07-11 11:39:37 +0200703 mdelay(rand_ms);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000704
wdenk3861aa52002-09-27 23:19:37 +0000705#endif /* CONFIG_BOOTP_RANDOM_DELAY */
706
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500707 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500708 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000709 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000710
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500711 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000712 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000713
714 /*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000715 * Next line results in incorrect packet size being transmitted,
716 * resulting in errors in some DHCP servers, reporting missing bytes.
717 * Size must be set in packet header after extension length has been
718 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000719 * C. Hallinan, DS4.COM, Inc.
720 */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000721 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500722 sizeof (struct bootp_hdr)); */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000723 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000724 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000725
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500726 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000727 bp->bp_op = OP_BOOTREQUEST;
728 bp->bp_htype = HWT_ETHER;
729 bp->bp_hlen = HWL_ETHER;
730 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +0200731 /*
732 * according to RFC1542, should be 0 on first request, secs since
733 * first request otherwise
734 */
735 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500736 zero_ip.s_addr = 0;
737 net_write_ip(&bp->bp_ciaddr, zero_ip);
738 net_write_ip(&bp->bp_yiaddr, zero_ip);
739 net_write_ip(&bp->bp_siaddr, zero_ip);
740 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500741 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger290c8992015-04-08 01:41:02 -0500742 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000743
744 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500745#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500746 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
747 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000748#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500749 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeligera9807e52007-07-10 11:05:02 -0500750#endif
wdenk3861aa52002-09-27 23:19:37 +0000751
752 /*
753 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200754 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000755 */
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500756 bootp_id = ((u32)net_ethaddr[2] << 24)
757 | ((u32)net_ethaddr[3] << 16)
758 | ((u32)net_ethaddr[4] << 8)
759 | (u32)net_ethaddr[5];
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500760 bootp_id += get_timer(0);
761 bootp_id = htonl(bootp_id);
762 bootp_add_id(bootp_id);
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500763 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000764
765 /*
766 * Calculate proper packet lengths taking into account the
767 * variable size of the options field
768 */
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000769 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
770 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500771 bcast_ip.s_addr = 0xFFFFFFFFL;
772 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500773 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000774
Jon Loeliger54f35c22007-07-09 17:45:14 -0500775#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000776 dhcp_state = SELECTING;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500777 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000778#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500779 net_set_udp_handler(bootp_handler);
Jon Loeligera9807e52007-07-10 11:05:02 -0500780#endif
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500781 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000782}
783
Jon Loeliger54f35c22007-07-09 17:45:14 -0500784#if defined(CONFIG_CMD_DHCP)
Stefan Brüns79f724c2015-09-04 00:31:49 +0200785static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000786{
wdenk3861aa52002-09-27 23:19:37 +0000787 int oplen, size;
Wolfgang Denk2b184222009-09-11 09:05:32 +0200788#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
789 int *to_ptr;
790#endif
wdenk3861aa52002-09-27 23:19:37 +0000791
wdenk29e7f5a2004-03-12 00:14:09 +0000792 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000793 oplen = *(popt + 1);
wdenk29e7f5a2004-03-12 00:14:09 +0000794 switch (*popt) {
Stefan Brüns44318292015-08-28 10:15:54 +0200795 case 0:
796 oplen = -1; /* Pad omits len byte */
797 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000798 case 1:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500799 net_copy_ip(&net_netmask, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000800 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500801#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000802 case 2: /* Time offset */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500803 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500804 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500805 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkb4ad9622005-04-01 00:25:43 +0000806 break;
807#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000808 case 3:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500809 net_copy_ip(&net_gateway, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000810 break;
811 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500812 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500813#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000814 if (*(popt + 1) > 4)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500815 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000816#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000817 break;
818 case 12:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000819 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500820 sizeof(net_hostname), oplen);
821 memcpy(&net_hostname, popt + 2, size);
822 net_hostname[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000823 break;
824 case 15: /* Ignore Domain Name Option */
825 break;
826 case 17:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000827 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500828 sizeof(net_root_path), oplen);
829 memcpy(&net_root_path, popt + 2, size);
830 net_root_path[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000831 break;
Brian Rzycki5a672a22012-09-11 09:22:53 +0000832 case 28: /* Ignore Broadcast Address Option */
833 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500834#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000835 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500836 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkb4ad9622005-04-01 00:25:43 +0000837 break;
838#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000839 case 51:
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500840 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000841 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200842 case 52:
843 dhcp_option_overload = popt[2];
844 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000845 case 53: /* Ignore Message Type Option */
846 break;
847 case 54:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500848 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000849 break;
850 case 58: /* Ignore Renewal Time Option */
851 break;
852 case 59: /* Ignore Rebinding Time Option */
853 break;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100854 case 66: /* Ignore TFTP server name */
855 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200856 case 67: /* Bootfile option */
857 size = truncate_sz("Bootfile",
858 sizeof(net_boot_file_name), oplen);
859 memcpy(&net_boot_file_name, popt + 2, size);
860 net_boot_file_name[size] = 0;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100861 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000862 default:
Jon Loeliger5336a762007-07-09 22:08:34 -0500863#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000864 if (dhcp_vendorex_proc(popt))
wdenk57b2d802003-06-27 21:31:46 +0000865 break;
wdenk3861aa52002-09-27 23:19:37 +0000866#endif
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000867 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500868 " %d\n", *popt);
wdenk29e7f5a2004-03-12 00:14:09 +0000869 break;
wdenk3861aa52002-09-27 23:19:37 +0000870 }
871 popt += oplen + 2; /* Process next option */
872 }
873}
874
Stefan Brüns79f724c2015-09-04 00:31:49 +0200875static void dhcp_packet_process_options(struct bootp_hdr *bp)
876{
877 uchar *popt = (uchar *)&bp->bp_vend[4];
878 uchar *end = popt + BOOTP_HDR_SIZE;
879
880 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
881 return;
882
883 dhcp_option_overload = 0;
884
885 /*
886 * The 'options' field MUST be interpreted first, 'file' next,
887 * 'sname' last.
888 */
889 dhcp_process_options(popt, end);
890
891 if (dhcp_option_overload & OVERLOAD_FILE) {
892 popt = (uchar *)bp->bp_file;
893 end = popt + sizeof(bp->bp_file);
894 dhcp_process_options(popt, end);
895 }
896
897 if (dhcp_option_overload & OVERLOAD_SNAME) {
898 popt = (uchar *)bp->bp_sname;
899 end = popt + sizeof(bp->bp_sname);
900 dhcp_process_options(popt, end);
901 }
902}
903
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500904static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +0000905{
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500906 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000907 return -1;
908
909 popt += 4;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000910 while (*popt != 0xff) {
911 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000912 return *(popt + 2);
Stefan Brüns44318292015-08-28 10:15:54 +0200913 if (*popt == 0) {
914 /* Pad */
915 popt += 1;
916 } else {
917 /* Scan through all options */
918 popt += *(popt + 1) + 2;
919 }
wdenk3861aa52002-09-27 23:19:37 +0000920 }
921 return -1;
922}
923
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500924static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000925{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000926 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500927 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000928 int pktlen, iplen, extlen;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000929 int eth_hdr_size;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500930 struct in_addr offered_ip;
931 struct in_addr zero_ip;
932 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000933
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500934 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500935 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000936 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000937
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500938 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000939 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000940
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000941 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000942 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000943
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500944 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000945 bp->bp_op = OP_BOOTREQUEST;
946 bp->bp_htype = HWT_ETHER;
947 bp->bp_hlen = HWL_ETHER;
948 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +0200949 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000950 /* Do not set the client IP, your IP, or server IP yet, since it
951 * hasn't been ACK'ed by the server yet */
Justin Flammia01f256b2007-10-29 17:40:35 -0400952
Wolfgang Denk8a933fb2006-10-12 00:01:08 +0200953 /*
Wolfgang Denk68e83a82006-10-09 01:26:14 +0200954 * RFC3046 requires Relay Agents to discard packets with
955 * nonzero and offered giaddr
956 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500957 zero_ip.s_addr = 0;
958 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denk68e83a82006-10-09 01:26:14 +0200959
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500960 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierad28a312016-01-28 11:19:02 -0500961 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000962
963 /*
964 * ID is the id of the OFFER packet
965 */
966
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500967 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +0000968
969 /*
970 * Copy options from OFFER packet if present
971 */
Justin Flammia01f256b2007-10-29 17:40:35 -0400972
973 /* Copy offered IP into the parameters request list */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500974 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
975 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
976 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +0000977
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000978 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
979 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500980 bcast_ip.s_addr = 0xFFFFFFFFL;
981 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000982
Aras Vaichas72aa3f32008-03-26 09:43:57 +1100983#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
984 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
985#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerb1e94762012-05-23 07:59:11 +0000986 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500987 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000988}
989
990/*
991 * Handle DHCP received packets.
992 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500993static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
994 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000995{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500996 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000997
Robin Getz9e0a4d62009-07-23 03:01:03 -0400998 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500999 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001000
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001001 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +02001002 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001003 return;
1004
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001005 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1006 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001007
Peng Fan67e72e92016-01-07 15:28:23 +08001008 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
1009 return;
1010
wdenk3861aa52002-09-27 23:19:37 +00001011 switch (dhcp_state) {
1012 case SELECTING:
1013 /*
1014 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001015 * to arrive. Then select one, and generate DHCPREQUEST
1016 * response. If filename is in format we recognize, assume it
1017 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001018 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001019 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001020#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001021 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001022 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001023 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001024#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Stefan Brüns79f724c2015-09-04 00:31:49 +02001025 dhcp_packet_process_options(bp);
wdenk3861aa52002-09-27 23:19:37 +00001026
Robin Getz9e0a4d62009-07-23 03:01:03 -04001027 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001028 dhcp_state = REQUESTING;
stroese5fa6e902003-04-10 13:26:44 +00001029
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001030 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001031 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001032#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001033 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001034#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001035
1036 return;
1037 break;
1038 case REQUESTING:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001039 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001040
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001041 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Stefan Brüns79f724c2015-09-04 00:31:49 +02001042 dhcp_packet_process_options(bp);
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001043 /* Store net params from reply */
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001044 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001045 dhcp_state = BOUND;
Thierry Reding8977cda2014-08-19 10:21:24 +02001046 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001047 &net_ip, get_timer(bootp_start));
Stefan Brüns4c60e4a2015-08-30 17:47:17 +02001048 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +00001049 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001050 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001051
Simon Glass5234ad12011-10-27 06:24:32 +00001052 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001053 return;
1054 }
1055 break;
Remy Bohmer03a44492008-08-20 11:30:28 +02001056 case BOUND:
1057 /* DHCP client bound to address */
1058 break;
wdenk3861aa52002-09-27 23:19:37 +00001059 default:
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001060 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001061 break;
1062 }
wdenk3861aa52002-09-27 23:19:37 +00001063}
1064
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001065void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001066{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001067 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001068}
Wolfgang Denkf7a7f082007-11-03 23:09:27 +01001069#endif /* CONFIG_CMD_DHCP */