blob: d718e35b63ec392e1f9d2df0cf1489df0e000d01 [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>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020013#include <efi_loader.h>
wdenk3861aa52002-09-27 23:19:37 +000014#include <net.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020015#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000016#include "bootp.h"
wdenk232c1502004-03-12 00:14:09 +000017#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000018#ifdef CONFIG_STATUS_LED
19#include <status_led.h>
20#endif
Kim Phillipsdb7720b2012-07-05 13:19:32 +000021#ifdef CONFIG_BOOTP_RANDOM_DELAY
22#include "net_rand.h"
23#endif
wdenk3861aa52002-09-27 23:19:37 +000024
Joe Hershberger3090b7e32012-05-15 08:59:06 +000025#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000026
Stephen Warrenf59be6e2014-07-25 17:30:48 -060027/*
28 * The timeout for the initial BOOTP/DHCP request used to be described by a
29 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
30 * that counter
31 *
32 * Now that the timeout periods are variable (exponential backoff and retry)
33 * we convert the timeout count to the absolute time it would have take to
34 * execute that many retries, and keep sending retry packets until that time
35 * is reached.
36 */
wdenk232c1502004-03-12 00:14:09 +000037#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000038# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000039#else
wdenk232c1502004-03-12 00:14:09 +000040# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000041#endif
Stephen Warrenf59be6e2014-07-25 17:30:48 -060042#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000043
Joe Hershberger3090b7e32012-05-15 08:59:06 +000044#define PORT_BOOTPS 67 /* BOOTP server UDP port */
45#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000046
Joe Hershberger3090b7e32012-05-15 08:59:06 +000047#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000048#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000049#endif
50
Thierry Reding92ac8ac2014-08-19 10:21:24 +020051#ifndef CONFIG_BOOTP_ID_CACHE_SIZE
52#define CONFIG_BOOTP_ID_CACHE_SIZE 4
53#endif
54
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -050055u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
Thierry Reding92ac8ac2014-08-19 10:21:24 +020056unsigned int bootp_num_ids;
Joe Hershberger7044c6b2015-04-08 01:41:09 -050057int bootp_try;
Stephen Warrenf59be6e2014-07-25 17:30:48 -060058ulong bootp_start;
59ulong bootp_timeout;
Joe Hershberger586cbe52015-04-08 01:41:03 -050060char net_nis_domain[32] = {0,}; /* Our NIS domain */
61char net_hostname[32] = {0,}; /* Our hostname */
62char net_root_path[64] = {0,}; /* Our bootpath */
wdenk3861aa52002-09-27 23:19:37 +000063
Alexandre Messier50768f52016-02-01 17:08:57 -050064static ulong time_taken_max;
65
Jon Loeliger643d1ab2007-07-09 17:45:14 -050066#if defined(CONFIG_CMD_DHCP)
Kim Phillips06370592012-10-29 13:34:33 +000067static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -050068static u32 dhcp_leasetime;
Joe Hershberger049a95a2015-04-08 01:41:01 -050069static struct in_addr dhcp_server_ip;
Stefan Brünsec87b1b2015-09-04 00:31:48 +020070static u8 dhcp_option_overload;
71#define OVERLOAD_FILE 1
72#define OVERLOAD_SNAME 2
Joe Hershberger049a95a2015-04-08 01:41:01 -050073static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
74 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000075
76/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000077#if 0
78static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000079{
80 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000081 case 1: return "DHCPDISCOVER"; break;
82 case 2: return "DHCPOFFER"; break;
83 case 3: return "DHCPREQUEST"; break;
84 case 4: return "DHCPDECLINE"; break;
85 case 5: return "DHCPACK"; break;
86 case 6: return "DHCPNACK"; break;
87 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000088 default: return "UNKNOWN/INVALID MSG TYPE"; break;
89 }
90}
wdenk3e386912003-04-05 00:53:31 +000091#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050092#endif
wdenk3861aa52002-09-27 23:19:37 +000093
Thierry Reding92ac8ac2014-08-19 10:21:24 +020094static void bootp_add_id(ulong id)
95{
96 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
97 size_t size = sizeof(bootp_ids) - sizeof(id);
98
99 memmove(bootp_ids, &bootp_ids[1], size);
100 bootp_ids[bootp_num_ids - 1] = id;
101 } else {
102 bootp_ids[bootp_num_ids] = id;
103 bootp_num_ids++;
104 }
105}
106
107static bool bootp_match_id(ulong id)
108{
109 unsigned int i;
110
111 for (i = 0; i < bootp_num_ids; i++)
112 if (bootp_ids[i] == id)
113 return true;
114
115 return false;
116}
117
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200118static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
119 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000120{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500121 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000122 int retval = 0;
123
124 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
125 retval = -1;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500126 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000127 retval = -2;
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200128 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000129 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000130 else if (bp->bp_htype != HWT_ETHER)
131 retval = -4;
132 else if (bp->bp_hlen != HWL_ETHER)
133 retval = -5;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500134 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000135 retval = -6;
Anton Persson214cc902016-03-17 09:38:21 +0100136 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
137 retval = -7;
wdenk3861aa52002-09-27 23:19:37 +0000138
Robin Getz0ebf04c2009-07-23 03:01:03 -0400139 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000140
141 return retval;
142}
143
144/*
145 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
146 */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500147static void store_net_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000148{
Wilson Callan5d110f02007-07-28 10:56:13 -0400149#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500150 struct in_addr tmp_ip;
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000151
Joe Hershberger049a95a2015-04-08 01:41:01 -0500152 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
153 if (tmp_ip.s_addr != 0)
154 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500155 memcpy(net_server_ethaddr,
156 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200157 if (
158#if defined(CONFIG_CMD_DHCP)
159 !(dhcp_option_overload & OVERLOAD_FILE) &&
160#endif
161 (strlen(bp->bp_file) > 0)) {
Joe Hershberger14111572015-04-08 01:41:02 -0500162 copy_filename(net_boot_file_name, bp->bp_file,
163 sizeof(net_boot_file_name));
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200164 }
wdenk3861aa52002-09-27 23:19:37 +0000165
Joe Hershberger14111572015-04-08 01:41:02 -0500166 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000167
168 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000169 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000170 * not contain a new value
171 */
Joe Hershberger14111572015-04-08 01:41:02 -0500172 if (*net_boot_file_name)
173 setenv("bootfile", net_boot_file_name);
Wu, Joshecec4e92014-11-18 13:07:08 +0800174#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500175 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000176}
177
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000178static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000179{
180 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000181 printf("*** WARNING: %s is too long (%d - max: %d)"
182 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000183 curlen = maxlen - 1;
184 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000185 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000186}
187
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500188#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000189
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500190static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000191{
wdenk232c1502004-03-12 00:14:09 +0000192 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000193
Robin Getz0ebf04c2009-07-23 03:01:03 -0400194 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500195 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000196
Joe Hershberger14111572015-04-08 01:41:02 -0500197 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000198
wdenk232c1502004-03-12 00:14:09 +0000199 switch (*ext) {
200 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000201 case 1: /* Subnet mask */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500202 if (net_netmask.s_addr == 0)
203 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000204 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000205 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000206 break;
wdenk232c1502004-03-12 00:14:09 +0000207 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000208 case 3: /* Gateways list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500209 if (net_gateway.s_addr == 0)
210 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000211 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000212 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000213 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000214 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000215 break;
216 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500217 if (net_dns_server.s_addr == 0)
218 net_copy_ip(&net_dns_server,
219 (struct in_addr *)(ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500220#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500221 if ((net_dns_server2.s_addr == 0) && (size > 4))
222 net_copy_ip(&net_dns_server2,
223 (struct in_addr *)(ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000224#endif
wdenk3861aa52002-09-27 23:19:37 +0000225 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000226 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000227 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000228 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000230 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000231 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000232 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000233 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000234 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000235 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000236 case 12: /* Host name */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500237 if (net_hostname[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000238 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500239 sizeof(net_hostname), size);
240 memcpy(&net_hostname, ext + 2, size);
241 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000242 }
243 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000244 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000245 if (size == 2)
Joe Hershberger14111572015-04-08 01:41:02 -0500246 net_boot_file_expected_size_in_blocks =
247 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000248 else if (size == 4)
Joe Hershberger14111572015-04-08 01:41:02 -0500249 net_boot_file_expected_size_in_blocks =
250 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000251 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000252 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000253 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000254 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000255 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000256 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000257 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000258 case 17: /* Root path */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500259 if (net_root_path[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000260 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500261 sizeof(net_root_path), size);
262 memcpy(&net_root_path, ext + 2, size);
263 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000264 }
265 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000266 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000267 /*
wdenk8bde7f72003-06-27 21:31:46 +0000268 * This can be used to send the information of the
269 * vendor area in another file that the client can
270 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000271 */
272 break;
wdenk232c1502004-03-12 00:14:09 +0000273 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000274 case 40: /* NIS Domain name */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500275 if (net_nis_domain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000276 size = truncate_sz("NIS Domain Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500277 sizeof(net_nis_domain), size);
278 memcpy(&net_nis_domain, ext + 2, size);
279 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000280 }
281 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000282#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
283 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500284 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000285 break;
286#endif
wdenk232c1502004-03-12 00:14:09 +0000287 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000288 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000289 /*
wdenk8bde7f72003-06-27 21:31:46 +0000290 * Binary information to exchange specific
291 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000292 */
293 break;
wdenk232c1502004-03-12 00:14:09 +0000294 /* Reserved (custom) fields (128..254) */
295 }
wdenk3861aa52002-09-27 23:19:37 +0000296}
297
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500298static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000299{
wdenk232c1502004-03-12 00:14:09 +0000300 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000301
Robin Getz0ebf04c2009-07-23 03:01:03 -0400302 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000303
wdenk232c1502004-03-12 00:14:09 +0000304 while ((ext < end) && (*ext != 0xff)) {
305 if (*ext == 0) {
306 ext++;
307 } else {
308 u8 *opt = ext;
309
310 ext += ext[1] + 2;
311 if (ext <= end)
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500312 bootp_process_vendor_field(opt);
wdenk232c1502004-03-12 00:14:09 +0000313 }
wdenk3861aa52002-09-27 23:19:37 +0000314 }
wdenk3861aa52002-09-27 23:19:37 +0000315
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000316 debug("[BOOTP] Received fields:\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500317 if (net_netmask.s_addr)
318 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000319
Joe Hershberger049a95a2015-04-08 01:41:01 -0500320 if (net_gateway.s_addr)
321 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000322
Joe Hershberger14111572015-04-08 01:41:02 -0500323 if (net_boot_file_expected_size_in_blocks)
324 debug("net_boot_file_expected_size_in_blocks : %d\n",
325 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000326
Joe Hershberger586cbe52015-04-08 01:41:03 -0500327 if (net_hostname[0])
328 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000329
Joe Hershberger586cbe52015-04-08 01:41:03 -0500330 if (net_root_path[0])
331 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000332
Joe Hershberger586cbe52015-04-08 01:41:03 -0500333 if (net_nis_domain[0])
334 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000335
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000336#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500337 if (net_ntp_server)
338 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000339#endif
wdenk3861aa52002-09-27 23:19:37 +0000340}
Simon Glass09349862011-06-13 16:13:12 -0700341
wdenk3861aa52002-09-27 23:19:37 +0000342/*
343 * Handle a BOOTP received packet.
344 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500345static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
346 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000347{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500348 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000349
Robin Getz0ebf04c2009-07-23 03:01:03 -0400350 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500351 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000352
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500353 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000354
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000355 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200356 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000357 return;
358
359 /*
wdenk232c1502004-03-12 00:14:09 +0000360 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000361 */
Thomas Chou82953322015-08-25 20:54:24 +0800362#if defined(CONFIG_STATUS_LED) && defined(STATUS_LED_BOOT)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000363 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000364#endif
365
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500366 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000367
368 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500369 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500370 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000371
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500372 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000373 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000374
Robin Getz0ebf04c2009-07-23 03:01:03 -0400375 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000376
Simon Glasse4a3d572011-10-27 06:24:32 +0000377 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000378}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500379#endif
wdenk3861aa52002-09-27 23:19:37 +0000380
381/*
382 * Timeout on BOOTP/DHCP request.
383 */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500384static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000385{
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600386 ulong time_taken = get_timer(bootp_start);
387
Alexandre Messier50768f52016-02-01 17:08:57 -0500388 if (time_taken >= time_taken_max) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000389#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600390 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000391 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000392#else
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600393 puts("\nRetry time exceeded; starting again\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500394 net_start_again();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000395#endif
wdenk3861aa52002-09-27 23:19:37 +0000396 } else {
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600397 bootp_timeout *= 2;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200398 if (bootp_timeout > 2000)
399 bootp_timeout = 2000;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500400 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500401 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000402 }
403}
404
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000405#define put_vci(e, str) \
406 do { \
407 size_t vci_strlen = strlen(str); \
408 *e++ = 60; /* Vendor Class Identifier */ \
409 *e++ = vci_strlen; \
410 memcpy(e, str, vci_strlen); \
411 e += vci_strlen; \
412 } while (0)
413
Alexander Graf4570a992016-05-06 21:01:02 +0200414static u8 *add_vci(u8 *e)
415{
416#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
417 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
418#elif defined(CONFIG_BOOTP_VCI_STRING)
419 put_vci(e, CONFIG_BOOTP_VCI_STRING);
420#endif
421
422 return e;
423}
424
wdenk3861aa52002-09-27 23:19:37 +0000425/*
426 * Initialize BOOTP extension fields in the request.
427 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500428#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500429static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
430 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000431{
wdenk232c1502004-03-12 00:14:09 +0000432 u8 *start = e;
433 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000434#if defined(CONFIG_BOOTP_PXE)
435 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000436 u16 clientarch;
437#endif
wdenk232c1502004-03-12 00:14:09 +0000438
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500439#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000440 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000441#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500442#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200443 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000444#endif
wdenk3861aa52002-09-27 23:19:37 +0000445
wdenk232c1502004-03-12 00:14:09 +0000446 *e++ = 99; /* RFC1048 Magic Cookie */
447 *e++ = 130;
448 *e++ = 83;
449 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000450
wdenk232c1502004-03-12 00:14:09 +0000451 *e++ = 53; /* DHCP Message Type */
452 *e++ = 1;
453 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000454
wdenk232c1502004-03-12 00:14:09 +0000455 *e++ = 57; /* Maximum DHCP Message Size */
456 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000457 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
458 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000459
Joe Hershberger049a95a2015-04-08 01:41:01 -0500460 if (server_ip.s_addr) {
461 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000462
wdenk232c1502004-03-12 00:14:09 +0000463 *e++ = 54; /* ServerID */
464 *e++ = 4;
465 *e++ = tmp >> 24;
466 *e++ = tmp >> 16;
467 *e++ = tmp >> 8;
468 *e++ = tmp & 0xff;
469 }
wdenk3861aa52002-09-27 23:19:37 +0000470
Joe Hershberger049a95a2015-04-08 01:41:01 -0500471 if (requested_ip.s_addr) {
472 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000473
wdenk232c1502004-03-12 00:14:09 +0000474 *e++ = 50; /* Requested IP */
475 *e++ = 4;
476 *e++ = tmp >> 24;
477 *e++ = tmp >> 16;
478 *e++ = tmp >> 8;
479 *e++ = tmp & 0xff;
480 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500481#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000482 hostname = getenv("hostname");
483 if (hostname) {
484 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000485
486 *e++ = 12; /* Hostname */
487 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000488 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000489 e += hostnamelen;
490 }
stroesefe389a82003-08-28 14:17:32 +0000491#endif
492
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000493#if defined(CONFIG_BOOTP_PXE)
494 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
495 *e++ = 93; /* Client System Architecture */
496 *e++ = 2;
497 *e++ = (clientarch >> 8) & 0xff;
498 *e++ = clientarch & 0xff;
499
500 *e++ = 94; /* Client Network Interface Identifier */
501 *e++ = 3;
502 *e++ = 1; /* type field for UNDI */
503 *e++ = 0; /* major revision */
504 *e++ = 0; /* minor revision */
505
506 uuid = getenv("pxeuuid");
507
508 if (uuid) {
509 if (uuid_str_valid(uuid)) {
510 *e++ = 97; /* Client Machine Identifier */
511 *e++ = 17;
512 *e++ = 0; /* type 0 - UUID */
513
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200514 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000515 e += 16;
516 } else {
517 printf("Invalid pxeuuid: %s\n", uuid);
518 }
519 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000520#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000521
Alexander Graf4570a992016-05-06 21:01:02 +0200522 e = add_vci(e);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000523
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500524#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000525 x = dhcp_vendorex_prep(e);
526 if (x)
wdenk232c1502004-03-12 00:14:09 +0000527 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000528#endif
529
wdenk232c1502004-03-12 00:14:09 +0000530 *e++ = 55; /* Parameter Request List */
531 cnt = e++; /* Pointer to count of requested items */
532 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500533#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000534 *e++ = 1; /* Subnet Mask */
535 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500537#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000538 *e++ = 2;
539 *cnt += 1;
540#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500541#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000542 *e++ = 3; /* Router Option */
543 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000544#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500545#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000546 *e++ = 6; /* DNS Server(s) */
547 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000548#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500549#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000550 *e++ = 12; /* Hostname */
551 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000552#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500553#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000554 *e++ = 13; /* Boot File Size */
555 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000556#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500557#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000558 *e++ = 17; /* Boot path */
559 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000560#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500561#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000562 *e++ = 40; /* NIS Domain name request */
563 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000564#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500565#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000566 *e++ = 42;
567 *cnt += 1;
568#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800569 /* no options, so back up to avoid sending an empty request list */
570 if (*cnt == 0)
571 e -= 2;
572
wdenk232c1502004-03-12 00:14:09 +0000573 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000574
wdenk232c1502004-03-12 00:14:09 +0000575 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000576#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800577 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000578 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000579#endif
580
wdenk232c1502004-03-12 00:14:09 +0000581 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000582}
583
Jon Loeliger610f2e92007-07-10 11:05:02 -0500584#else
wdenk3861aa52002-09-27 23:19:37 +0000585/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000586 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000587 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500588static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000589{
wdenk232c1502004-03-12 00:14:09 +0000590 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000591
wdenk232c1502004-03-12 00:14:09 +0000592 *e++ = 99; /* RFC1048 Magic Cookie */
593 *e++ = 130;
594 *e++ = 83;
595 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000596
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500597#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000598 *e++ = 53; /* DHCP Message Type */
599 *e++ = 1;
600 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000601
wdenk232c1502004-03-12 00:14:09 +0000602 *e++ = 57; /* Maximum DHCP Message Size */
603 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000604 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
605 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500606#endif
wdenk3861aa52002-09-27 23:19:37 +0000607
Alexander Graf4570a992016-05-06 21:01:02 +0200608 add_vci(e);
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000609
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500610#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000611 *e++ = 1; /* Subnet mask request */
612 *e++ = 4;
613 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000614#endif
615
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500616#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000617 *e++ = 3; /* Default gateway request */
618 *e++ = 4;
619 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000620#endif
621
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500622#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000623 *e++ = 6; /* Domain Name Server */
624 *e++ = 4;
625 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000626#endif
627
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500628#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000629 *e++ = 12; /* Host name request */
630 *e++ = 32;
631 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000632#endif
633
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500634#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000635 *e++ = 13; /* Boot file size */
636 *e++ = 2;
637 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000638#endif
639
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500640#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000641 *e++ = 17; /* Boot path */
642 *e++ = 32;
643 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000644#endif
645
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500646#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000647 *e++ = 40; /* NIS Domain name request */
648 *e++ = 32;
649 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000650#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000651#if defined(CONFIG_BOOTP_NTPSERVER)
652 *e++ = 42;
653 *e++ = 4;
654 e += 4;
655#endif
wdenk3861aa52002-09-27 23:19:37 +0000656
wdenk232c1502004-03-12 00:14:09 +0000657 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000658
wdenk232c1502004-03-12 00:14:09 +0000659 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000660}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500661#endif
wdenk3861aa52002-09-27 23:19:37 +0000662
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500663void bootp_reset(void)
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600664{
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200665 bootp_num_ids = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500666 bootp_try = 0;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600667 bootp_start = get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200668 bootp_timeout = 250;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600669}
670
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500671void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000672{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000673 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500674 struct bootp_hdr *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000675 int extlen, pktlen, iplen;
676 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000677#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200678 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000679#endif
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500680 u32 bootp_id;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500681 struct in_addr zero_ip;
682 struct in_addr bcast_ip;
Alexandre Messier50768f52016-02-01 17:08:57 -0500683 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000684
Simon Glass573f14f2011-12-10 11:08:06 +0000685 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500686#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000687 dhcp_state = INIT;
688#endif
689
Alexandre Messier50768f52016-02-01 17:08:57 -0500690 ep = getenv("bootpretryperiod");
691 if (ep != NULL)
692 time_taken_max = simple_strtoul(ep, NULL, 10);
693 else
694 time_taken_max = TIMEOUT_MS;
695
wdenk3861aa52002-09-27 23:19:37 +0000696#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500697 if (bootp_try == 0)
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000698 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000699
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500700 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
701 rand_ms = rand() >> (22 - bootp_try);
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000702 else /* After 3rd BOOTP request max 8192 * 1ms */
703 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000704
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000705 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200706 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000707
wdenk3861aa52002-09-27 23:19:37 +0000708#endif /* CONFIG_BOOTP_RANDOM_DELAY */
709
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500710 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500711 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000712 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000713
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500714 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000715 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000716
717 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000718 * Next line results in incorrect packet size being transmitted,
719 * resulting in errors in some DHCP servers, reporting missing bytes.
720 * Size must be set in packet header after extension length has been
721 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000722 * C. Hallinan, DS4.COM, Inc.
723 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000724 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500725 sizeof (struct bootp_hdr)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000726 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000727 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000728
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500729 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000730 bp->bp_op = OP_BOOTREQUEST;
731 bp->bp_htype = HWT_ETHER;
732 bp->bp_hlen = HWL_ETHER;
733 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200734 /*
735 * according to RFC1542, should be 0 on first request, secs since
736 * first request otherwise
737 */
738 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500739 zero_ip.s_addr = 0;
740 net_write_ip(&bp->bp_ciaddr, zero_ip);
741 net_write_ip(&bp->bp_yiaddr, zero_ip);
742 net_write_ip(&bp->bp_siaddr, zero_ip);
743 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500744 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger14111572015-04-08 01:41:02 -0500745 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000746
747 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500748#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500749 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
750 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000751#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500752 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500753#endif
wdenk3861aa52002-09-27 23:19:37 +0000754
755 /*
756 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200757 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000758 */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500759 bootp_id = ((u32)net_ethaddr[2] << 24)
760 | ((u32)net_ethaddr[3] << 16)
761 | ((u32)net_ethaddr[4] << 8)
762 | (u32)net_ethaddr[5];
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500763 bootp_id += get_timer(0);
764 bootp_id = htonl(bootp_id);
765 bootp_add_id(bootp_id);
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500766 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000767
768 /*
769 * Calculate proper packet lengths taking into account the
770 * variable size of the options field
771 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000772 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
773 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500774 bcast_ip.s_addr = 0xFFFFFFFFL;
775 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500776 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000777
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500778#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000779 dhcp_state = SELECTING;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500780 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000781#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500782 net_set_udp_handler(bootp_handler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500783#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500784 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000785}
786
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500787#if defined(CONFIG_CMD_DHCP)
Stefan Brüns774c3e02015-09-04 00:31:49 +0200788static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000789{
wdenk3861aa52002-09-27 23:19:37 +0000790 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200791#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
792 int *to_ptr;
793#endif
wdenk3861aa52002-09-27 23:19:37 +0000794
wdenk232c1502004-03-12 00:14:09 +0000795 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000796 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000797 switch (*popt) {
Stefan Brünsc56eb572015-08-28 10:15:54 +0200798 case 0:
799 oplen = -1; /* Pad omits len byte */
800 break;
wdenk232c1502004-03-12 00:14:09 +0000801 case 1:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500802 net_copy_ip(&net_netmask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000803 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500804#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000805 case 2: /* Time offset */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500806 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500807 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500808 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkea287de2005-04-01 00:25:43 +0000809 break;
810#endif
wdenk232c1502004-03-12 00:14:09 +0000811 case 3:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500812 net_copy_ip(&net_gateway, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000813 break;
814 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500815 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500816#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000817 if (*(popt + 1) > 4)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500818 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000819#endif
wdenk232c1502004-03-12 00:14:09 +0000820 break;
821 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000822 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500823 sizeof(net_hostname), oplen);
824 memcpy(&net_hostname, popt + 2, size);
825 net_hostname[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000826 break;
827 case 15: /* Ignore Domain Name Option */
828 break;
829 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000830 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500831 sizeof(net_root_path), oplen);
832 memcpy(&net_root_path, popt + 2, size);
833 net_root_path[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000834 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000835 case 28: /* Ignore Broadcast Address Option */
836 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500837#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000838 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500839 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000840 break;
841#endif
wdenk232c1502004-03-12 00:14:09 +0000842 case 51:
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500843 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000844 break;
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200845 case 52:
846 dhcp_option_overload = popt[2];
847 break;
wdenk232c1502004-03-12 00:14:09 +0000848 case 53: /* Ignore Message Type Option */
849 break;
850 case 54:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500851 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000852 break;
853 case 58: /* Ignore Renewal Time Option */
854 break;
855 case 59: /* Ignore Rebinding Time Option */
856 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100857 case 66: /* Ignore TFTP server name */
858 break;
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200859 case 67: /* Bootfile option */
860 size = truncate_sz("Bootfile",
861 sizeof(net_boot_file_name), oplen);
862 memcpy(&net_boot_file_name, popt + 2, size);
863 net_boot_file_name[size] = 0;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100864 break;
wdenk232c1502004-03-12 00:14:09 +0000865 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500866#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000867 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000868 break;
wdenk3861aa52002-09-27 23:19:37 +0000869#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000870 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500871 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000872 break;
wdenk3861aa52002-09-27 23:19:37 +0000873 }
874 popt += oplen + 2; /* Process next option */
875 }
876}
877
Stefan Brüns774c3e02015-09-04 00:31:49 +0200878static void dhcp_packet_process_options(struct bootp_hdr *bp)
879{
880 uchar *popt = (uchar *)&bp->bp_vend[4];
881 uchar *end = popt + BOOTP_HDR_SIZE;
882
883 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
884 return;
885
886 dhcp_option_overload = 0;
887
888 /*
889 * The 'options' field MUST be interpreted first, 'file' next,
890 * 'sname' last.
891 */
892 dhcp_process_options(popt, end);
893
894 if (dhcp_option_overload & OVERLOAD_FILE) {
895 popt = (uchar *)bp->bp_file;
896 end = popt + sizeof(bp->bp_file);
897 dhcp_process_options(popt, end);
898 }
899
900 if (dhcp_option_overload & OVERLOAD_SNAME) {
901 popt = (uchar *)bp->bp_sname;
902 end = popt + sizeof(bp->bp_sname);
903 dhcp_process_options(popt, end);
904 }
905}
906
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500907static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +0000908{
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500909 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000910 return -1;
911
912 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000913 while (*popt != 0xff) {
914 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000915 return *(popt + 2);
Stefan Brünsc56eb572015-08-28 10:15:54 +0200916 if (*popt == 0) {
917 /* Pad */
918 popt += 1;
919 } else {
920 /* Scan through all options */
921 popt += *(popt + 1) + 2;
922 }
wdenk3861aa52002-09-27 23:19:37 +0000923 }
924 return -1;
925}
926
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500927static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000928{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000929 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500930 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000931 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000932 int eth_hdr_size;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500933 struct in_addr offered_ip;
934 struct in_addr zero_ip;
935 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000936
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500937 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500938 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000939 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000940
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500941 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000942 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000943
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000944 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000945 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000946
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500947 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000948 bp->bp_op = OP_BOOTREQUEST;
949 bp->bp_htype = HWT_ETHER;
950 bp->bp_hlen = HWL_ETHER;
951 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200952 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000953 /* Do not set the client IP, your IP, or server IP yet, since it
954 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400955
Wolfgang Denkc6686702006-10-12 00:01:08 +0200956 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200957 * RFC3046 requires Relay Agents to discard packets with
958 * nonzero and offered giaddr
959 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500960 zero_ip.s_addr = 0;
961 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200962
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500963 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierb2b7fbc2016-01-28 11:19:02 -0500964 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000965
966 /*
967 * ID is the id of the OFFER packet
968 */
969
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500970 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +0000971
972 /*
973 * Copy options from OFFER packet if present
974 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400975
976 /* Copy offered IP into the parameters request list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500977 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
978 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
979 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +0000980
Joe Hershbergerae446f52012-05-23 07:59:10 +0000981 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
982 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500983 bcast_ip.s_addr = 0xFFFFFFFFL;
984 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000985
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100986#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
987 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
988#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000989 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500990 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000991}
992
993/*
994 * Handle DHCP received packets.
995 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500996static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
997 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000998{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500999 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001000
Robin Getz0ebf04c2009-07-23 03:01:03 -04001001 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001002 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001003
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001004 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +02001005 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001006 return;
1007
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001008 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1009 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001010
Peng Fan44c42dd2016-01-07 15:28:23 +08001011 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
1012 return;
1013
wdenk3861aa52002-09-27 23:19:37 +00001014 switch (dhcp_state) {
1015 case SELECTING:
1016 /*
1017 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001018 * to arrive. Then select one, and generate DHCPREQUEST
1019 * response. If filename is in format we recognize, assume it
1020 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001021 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001022 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001023#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001024 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001025 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001026 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001027#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Stefan Brüns774c3e02015-09-04 00:31:49 +02001028 dhcp_packet_process_options(bp);
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001029 efi_net_set_dhcp_ack(pkt, len);
wdenk3861aa52002-09-27 23:19:37 +00001030
Robin Getz0ebf04c2009-07-23 03:01:03 -04001031 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001032 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +00001033
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001034 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001035 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001036#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001037 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001038#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001039
1040 return;
1041 break;
1042 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001043 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001044
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001045 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Stefan Brüns774c3e02015-09-04 00:31:49 +02001046 dhcp_packet_process_options(bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001047 /* Store net params from reply */
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001048 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001049 dhcp_state = BOUND;
Thierry Reding92ac8ac2014-08-19 10:21:24 +02001050 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001051 &net_ip, get_timer(bootp_start));
Stefan Brüns4f28c9b2015-08-30 17:47:17 +02001052 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +00001053 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001054 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001055
Simon Glasse4a3d572011-10-27 06:24:32 +00001056 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001057 return;
1058 }
1059 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +02001060 case BOUND:
1061 /* DHCP client bound to address */
1062 break;
wdenk3861aa52002-09-27 23:19:37 +00001063 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001064 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001065 break;
1066 }
wdenk3861aa52002-09-27 23:19:37 +00001067}
1068
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001069void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001070{
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001071 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001072}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001073#endif /* CONFIG_CMD_DHCP */