blob: 85dc524df4fdd8d3e8549b6bab751ab5aa24d0ff [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{
Alexander Graf20898ea2016-05-06 21:01:07 +0200416 char *vci = NULL;
417 char *env_vci = getenv("bootp_vci");
418
Alexander Graf4570a992016-05-06 21:01:02 +0200419#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
Alexander Graf20898ea2016-05-06 21:01:07 +0200420 vci = CONFIG_SPL_NET_VCI_STRING;
Alexander Graf4570a992016-05-06 21:01:02 +0200421#elif defined(CONFIG_BOOTP_VCI_STRING)
Alexander Graf20898ea2016-05-06 21:01:07 +0200422 vci = CONFIG_BOOTP_VCI_STRING;
Alexander Graf4570a992016-05-06 21:01:02 +0200423#endif
424
Alexander Graf20898ea2016-05-06 21:01:07 +0200425 if (env_vci)
426 vci = env_vci;
427
428 if (vci)
429 put_vci(e, vci);
430
Alexander Graf4570a992016-05-06 21:01:02 +0200431 return e;
432}
433
wdenk3861aa52002-09-27 23:19:37 +0000434/*
435 * Initialize BOOTP extension fields in the request.
436 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500437#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500438static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
439 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000440{
wdenk232c1502004-03-12 00:14:09 +0000441 u8 *start = e;
442 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000443#if defined(CONFIG_BOOTP_PXE)
444 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000445 u16 clientarch;
446#endif
wdenk232c1502004-03-12 00:14:09 +0000447
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500448#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000449 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000450#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500451#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200452 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000453#endif
wdenk3861aa52002-09-27 23:19:37 +0000454
wdenk232c1502004-03-12 00:14:09 +0000455 *e++ = 99; /* RFC1048 Magic Cookie */
456 *e++ = 130;
457 *e++ = 83;
458 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000459
wdenk232c1502004-03-12 00:14:09 +0000460 *e++ = 53; /* DHCP Message Type */
461 *e++ = 1;
462 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000463
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 57; /* Maximum DHCP Message Size */
465 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000466 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
467 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000468
Joe Hershberger049a95a2015-04-08 01:41:01 -0500469 if (server_ip.s_addr) {
470 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000471
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 54; /* ServerID */
473 *e++ = 4;
474 *e++ = tmp >> 24;
475 *e++ = tmp >> 16;
476 *e++ = tmp >> 8;
477 *e++ = tmp & 0xff;
478 }
wdenk3861aa52002-09-27 23:19:37 +0000479
Joe Hershberger049a95a2015-04-08 01:41:01 -0500480 if (requested_ip.s_addr) {
481 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000482
wdenk232c1502004-03-12 00:14:09 +0000483 *e++ = 50; /* Requested IP */
484 *e++ = 4;
485 *e++ = tmp >> 24;
486 *e++ = tmp >> 16;
487 *e++ = tmp >> 8;
488 *e++ = tmp & 0xff;
489 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500490#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000491 hostname = getenv("hostname");
492 if (hostname) {
493 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000494
495 *e++ = 12; /* Hostname */
496 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000497 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000498 e += hostnamelen;
499 }
stroesefe389a82003-08-28 14:17:32 +0000500#endif
501
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000502#if defined(CONFIG_BOOTP_PXE)
503 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
504 *e++ = 93; /* Client System Architecture */
505 *e++ = 2;
506 *e++ = (clientarch >> 8) & 0xff;
507 *e++ = clientarch & 0xff;
508
509 *e++ = 94; /* Client Network Interface Identifier */
510 *e++ = 3;
511 *e++ = 1; /* type field for UNDI */
512 *e++ = 0; /* major revision */
513 *e++ = 0; /* minor revision */
514
515 uuid = getenv("pxeuuid");
516
517 if (uuid) {
518 if (uuid_str_valid(uuid)) {
519 *e++ = 97; /* Client Machine Identifier */
520 *e++ = 17;
521 *e++ = 0; /* type 0 - UUID */
522
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200523 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000524 e += 16;
525 } else {
526 printf("Invalid pxeuuid: %s\n", uuid);
527 }
528 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000529#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000530
Alexander Graf4570a992016-05-06 21:01:02 +0200531 e = add_vci(e);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000532
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500533#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000534 x = dhcp_vendorex_prep(e);
535 if (x)
wdenk232c1502004-03-12 00:14:09 +0000536 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000537#endif
538
wdenk232c1502004-03-12 00:14:09 +0000539 *e++ = 55; /* Parameter Request List */
540 cnt = e++; /* Pointer to count of requested items */
541 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500542#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000543 *e++ = 1; /* Subnet Mask */
544 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000545#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500546#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000547 *e++ = 2;
548 *cnt += 1;
549#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500550#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000551 *e++ = 3; /* Router Option */
552 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000553#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500554#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000555 *e++ = 6; /* DNS Server(s) */
556 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000557#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500558#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000559 *e++ = 12; /* Hostname */
560 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000561#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500562#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000563 *e++ = 13; /* Boot File Size */
564 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000565#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500566#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000567 *e++ = 17; /* Boot path */
568 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000569#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500570#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000571 *e++ = 40; /* NIS Domain name request */
572 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000573#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500574#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000575 *e++ = 42;
576 *cnt += 1;
577#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800578 /* no options, so back up to avoid sending an empty request list */
579 if (*cnt == 0)
580 e -= 2;
581
wdenk232c1502004-03-12 00:14:09 +0000582 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000583
wdenk232c1502004-03-12 00:14:09 +0000584 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000585#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800586 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000587 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000588#endif
589
wdenk232c1502004-03-12 00:14:09 +0000590 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000591}
592
Jon Loeliger610f2e92007-07-10 11:05:02 -0500593#else
wdenk3861aa52002-09-27 23:19:37 +0000594/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000595 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000596 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500597static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000598{
wdenk232c1502004-03-12 00:14:09 +0000599 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000600
wdenk232c1502004-03-12 00:14:09 +0000601 *e++ = 99; /* RFC1048 Magic Cookie */
602 *e++ = 130;
603 *e++ = 83;
604 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000605
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500606#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000607 *e++ = 53; /* DHCP Message Type */
608 *e++ = 1;
609 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000610
wdenk232c1502004-03-12 00:14:09 +0000611 *e++ = 57; /* Maximum DHCP Message Size */
612 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000613 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
614 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500615#endif
wdenk3861aa52002-09-27 23:19:37 +0000616
Alexander Graf4570a992016-05-06 21:01:02 +0200617 add_vci(e);
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000618
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500619#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000620 *e++ = 1; /* Subnet mask request */
621 *e++ = 4;
622 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000623#endif
624
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500625#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000626 *e++ = 3; /* Default gateway request */
627 *e++ = 4;
628 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000629#endif
630
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500631#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000632 *e++ = 6; /* Domain Name Server */
633 *e++ = 4;
634 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000635#endif
636
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500637#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000638 *e++ = 12; /* Host name request */
639 *e++ = 32;
640 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000641#endif
642
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500643#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000644 *e++ = 13; /* Boot file size */
645 *e++ = 2;
646 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000647#endif
648
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500649#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000650 *e++ = 17; /* Boot path */
651 *e++ = 32;
652 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000653#endif
654
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500655#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000656 *e++ = 40; /* NIS Domain name request */
657 *e++ = 32;
658 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000659#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000660#if defined(CONFIG_BOOTP_NTPSERVER)
661 *e++ = 42;
662 *e++ = 4;
663 e += 4;
664#endif
wdenk3861aa52002-09-27 23:19:37 +0000665
wdenk232c1502004-03-12 00:14:09 +0000666 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000667
wdenk232c1502004-03-12 00:14:09 +0000668 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000669}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500670#endif
wdenk3861aa52002-09-27 23:19:37 +0000671
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500672void bootp_reset(void)
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600673{
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200674 bootp_num_ids = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500675 bootp_try = 0;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600676 bootp_start = get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200677 bootp_timeout = 250;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600678}
679
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500680void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000681{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000682 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500683 struct bootp_hdr *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000684 int extlen, pktlen, iplen;
685 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000686#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200687 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000688#endif
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500689 u32 bootp_id;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500690 struct in_addr zero_ip;
691 struct in_addr bcast_ip;
Alexandre Messier50768f52016-02-01 17:08:57 -0500692 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000693
Simon Glass573f14f2011-12-10 11:08:06 +0000694 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500695#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000696 dhcp_state = INIT;
697#endif
698
Alexandre Messier50768f52016-02-01 17:08:57 -0500699 ep = getenv("bootpretryperiod");
700 if (ep != NULL)
701 time_taken_max = simple_strtoul(ep, NULL, 10);
702 else
703 time_taken_max = TIMEOUT_MS;
704
wdenk3861aa52002-09-27 23:19:37 +0000705#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500706 if (bootp_try == 0)
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000707 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000708
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500709 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
710 rand_ms = rand() >> (22 - bootp_try);
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000711 else /* After 3rd BOOTP request max 8192 * 1ms */
712 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000713
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000714 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200715 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000716
wdenk3861aa52002-09-27 23:19:37 +0000717#endif /* CONFIG_BOOTP_RANDOM_DELAY */
718
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500719 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500720 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000721 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000722
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500723 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000724 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000725
726 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000727 * Next line results in incorrect packet size being transmitted,
728 * resulting in errors in some DHCP servers, reporting missing bytes.
729 * Size must be set in packet header after extension length has been
730 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000731 * C. Hallinan, DS4.COM, Inc.
732 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000733 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500734 sizeof (struct bootp_hdr)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000735 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000736 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000737
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500738 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000739 bp->bp_op = OP_BOOTREQUEST;
740 bp->bp_htype = HWT_ETHER;
741 bp->bp_hlen = HWL_ETHER;
742 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200743 /*
744 * according to RFC1542, should be 0 on first request, secs since
745 * first request otherwise
746 */
747 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500748 zero_ip.s_addr = 0;
749 net_write_ip(&bp->bp_ciaddr, zero_ip);
750 net_write_ip(&bp->bp_yiaddr, zero_ip);
751 net_write_ip(&bp->bp_siaddr, zero_ip);
752 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500753 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger14111572015-04-08 01:41:02 -0500754 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000755
756 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500757#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500758 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
759 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000760#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500761 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500762#endif
wdenk3861aa52002-09-27 23:19:37 +0000763
764 /*
765 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200766 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000767 */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500768 bootp_id = ((u32)net_ethaddr[2] << 24)
769 | ((u32)net_ethaddr[3] << 16)
770 | ((u32)net_ethaddr[4] << 8)
771 | (u32)net_ethaddr[5];
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500772 bootp_id += get_timer(0);
773 bootp_id = htonl(bootp_id);
774 bootp_add_id(bootp_id);
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500775 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000776
777 /*
778 * Calculate proper packet lengths taking into account the
779 * variable size of the options field
780 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000781 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
782 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500783 bcast_ip.s_addr = 0xFFFFFFFFL;
784 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500785 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000786
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500787#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000788 dhcp_state = SELECTING;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500789 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000790#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500791 net_set_udp_handler(bootp_handler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500792#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500793 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000794}
795
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500796#if defined(CONFIG_CMD_DHCP)
Stefan Brüns774c3e02015-09-04 00:31:49 +0200797static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000798{
wdenk3861aa52002-09-27 23:19:37 +0000799 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200800#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
801 int *to_ptr;
802#endif
wdenk3861aa52002-09-27 23:19:37 +0000803
wdenk232c1502004-03-12 00:14:09 +0000804 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000805 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000806 switch (*popt) {
Stefan Brünsc56eb572015-08-28 10:15:54 +0200807 case 0:
808 oplen = -1; /* Pad omits len byte */
809 break;
wdenk232c1502004-03-12 00:14:09 +0000810 case 1:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500811 net_copy_ip(&net_netmask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000812 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500813#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000814 case 2: /* Time offset */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500815 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500816 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500817 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkea287de2005-04-01 00:25:43 +0000818 break;
819#endif
wdenk232c1502004-03-12 00:14:09 +0000820 case 3:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500821 net_copy_ip(&net_gateway, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000822 break;
823 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500824 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500825#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000826 if (*(popt + 1) > 4)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500827 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000828#endif
wdenk232c1502004-03-12 00:14:09 +0000829 break;
830 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000831 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500832 sizeof(net_hostname), oplen);
833 memcpy(&net_hostname, popt + 2, size);
834 net_hostname[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000835 break;
836 case 15: /* Ignore Domain Name Option */
837 break;
838 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000839 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500840 sizeof(net_root_path), oplen);
841 memcpy(&net_root_path, popt + 2, size);
842 net_root_path[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000843 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000844 case 28: /* Ignore Broadcast Address Option */
845 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500846#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000847 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500848 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000849 break;
850#endif
wdenk232c1502004-03-12 00:14:09 +0000851 case 51:
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500852 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000853 break;
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200854 case 52:
855 dhcp_option_overload = popt[2];
856 break;
wdenk232c1502004-03-12 00:14:09 +0000857 case 53: /* Ignore Message Type Option */
858 break;
859 case 54:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500860 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000861 break;
862 case 58: /* Ignore Renewal Time Option */
863 break;
864 case 59: /* Ignore Rebinding Time Option */
865 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100866 case 66: /* Ignore TFTP server name */
867 break;
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200868 case 67: /* Bootfile option */
869 size = truncate_sz("Bootfile",
870 sizeof(net_boot_file_name), oplen);
871 memcpy(&net_boot_file_name, popt + 2, size);
872 net_boot_file_name[size] = 0;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100873 break;
wdenk232c1502004-03-12 00:14:09 +0000874 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500875#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000876 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000877 break;
wdenk3861aa52002-09-27 23:19:37 +0000878#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000879 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500880 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000881 break;
wdenk3861aa52002-09-27 23:19:37 +0000882 }
883 popt += oplen + 2; /* Process next option */
884 }
885}
886
Stefan Brüns774c3e02015-09-04 00:31:49 +0200887static void dhcp_packet_process_options(struct bootp_hdr *bp)
888{
889 uchar *popt = (uchar *)&bp->bp_vend[4];
890 uchar *end = popt + BOOTP_HDR_SIZE;
891
892 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
893 return;
894
895 dhcp_option_overload = 0;
896
897 /*
898 * The 'options' field MUST be interpreted first, 'file' next,
899 * 'sname' last.
900 */
901 dhcp_process_options(popt, end);
902
903 if (dhcp_option_overload & OVERLOAD_FILE) {
904 popt = (uchar *)bp->bp_file;
905 end = popt + sizeof(bp->bp_file);
906 dhcp_process_options(popt, end);
907 }
908
909 if (dhcp_option_overload & OVERLOAD_SNAME) {
910 popt = (uchar *)bp->bp_sname;
911 end = popt + sizeof(bp->bp_sname);
912 dhcp_process_options(popt, end);
913 }
914}
915
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500916static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +0000917{
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500918 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000919 return -1;
920
921 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000922 while (*popt != 0xff) {
923 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000924 return *(popt + 2);
Stefan Brünsc56eb572015-08-28 10:15:54 +0200925 if (*popt == 0) {
926 /* Pad */
927 popt += 1;
928 } else {
929 /* Scan through all options */
930 popt += *(popt + 1) + 2;
931 }
wdenk3861aa52002-09-27 23:19:37 +0000932 }
933 return -1;
934}
935
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500936static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000937{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000938 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500939 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000940 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000941 int eth_hdr_size;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500942 struct in_addr offered_ip;
943 struct in_addr zero_ip;
944 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000945
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500946 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500947 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000948 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000949
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500950 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000951 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000952
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000953 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000954 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000955
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500956 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000957 bp->bp_op = OP_BOOTREQUEST;
958 bp->bp_htype = HWT_ETHER;
959 bp->bp_hlen = HWL_ETHER;
960 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200961 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000962 /* Do not set the client IP, your IP, or server IP yet, since it
963 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400964
Wolfgang Denkc6686702006-10-12 00:01:08 +0200965 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200966 * RFC3046 requires Relay Agents to discard packets with
967 * nonzero and offered giaddr
968 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500969 zero_ip.s_addr = 0;
970 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200971
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500972 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierb2b7fbc2016-01-28 11:19:02 -0500973 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000974
975 /*
976 * ID is the id of the OFFER packet
977 */
978
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500979 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +0000980
981 /*
982 * Copy options from OFFER packet if present
983 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400984
985 /* Copy offered IP into the parameters request list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500986 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
987 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
988 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +0000989
Joe Hershbergerae446f52012-05-23 07:59:10 +0000990 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
991 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500992 bcast_ip.s_addr = 0xFFFFFFFFL;
993 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000994
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100995#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
996 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
997#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000998 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500999 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +00001000}
1001
1002/*
1003 * Handle DHCP received packets.
1004 */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001005static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1006 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +00001007{
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001008 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001009
Robin Getz0ebf04c2009-07-23 03:01:03 -04001010 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001011 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001012
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001013 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +02001014 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001015 return;
1016
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001017 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1018 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001019
Peng Fan44c42dd2016-01-07 15:28:23 +08001020 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
1021 return;
1022
wdenk3861aa52002-09-27 23:19:37 +00001023 switch (dhcp_state) {
1024 case SELECTING:
1025 /*
1026 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001027 * to arrive. Then select one, and generate DHCPREQUEST
1028 * response. If filename is in format we recognize, assume it
1029 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001030 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001031 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001032#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001033 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001034 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001035 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001036#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Stefan Brüns774c3e02015-09-04 00:31:49 +02001037 dhcp_packet_process_options(bp);
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001038 efi_net_set_dhcp_ack(pkt, len);
wdenk3861aa52002-09-27 23:19:37 +00001039
Robin Getz0ebf04c2009-07-23 03:01:03 -04001040 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001041 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +00001042
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001043 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001044 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001045#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001046 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001047#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001048
1049 return;
1050 break;
1051 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001052 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001053
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001054 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Stefan Brüns774c3e02015-09-04 00:31:49 +02001055 dhcp_packet_process_options(bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001056 /* Store net params from reply */
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001057 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001058 dhcp_state = BOUND;
Thierry Reding92ac8ac2014-08-19 10:21:24 +02001059 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001060 &net_ip, get_timer(bootp_start));
Stefan Brüns4f28c9b2015-08-30 17:47:17 +02001061 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +00001062 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001063 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001064
Simon Glasse4a3d572011-10-27 06:24:32 +00001065 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001066 return;
1067 }
1068 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +02001069 case BOUND:
1070 /* DHCP client bound to address */
1071 break;
wdenk3861aa52002-09-27 23:19:37 +00001072 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001073 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001074 break;
1075 }
wdenk3861aa52002-09-27 23:19:37 +00001076}
1077
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001078void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001079{
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001080 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001081}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001082#endif /* CONFIG_CMD_DHCP */