blob: 95d906e3b2d28f937e4ff7d04fd4b796475839a2 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk29e7f5a2004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.h>
wdenk3861aa52002-09-27 23:19:37 +000012#include <command.h>
Simon Glass6eaea252019-08-01 09:46:48 -060013#include <env.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020014#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
wdenk3861aa52002-09-27 23:19:37 +000016#include <net.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <rand.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010018#include <u-boot/uuid.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020020#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000021#include "bootp.h"
Uri Mashiach4892d392017-01-19 10:51:45 +020022#ifdef CONFIG_LED_STATUS
wdenk3861aa52002-09-27 23:19:37 +000023#include <status_led.h>
24#endif
Kim Phillips576175b2012-07-05 13:19:32 +000025#ifdef CONFIG_BOOTP_RANDOM_DELAY
26#include "net_rand.h"
27#endif
Sean Edmond57867112023-07-25 16:20:30 -070028#include <malloc.h>
wdenk3861aa52002-09-27 23:19:37 +000029
Joe Hershberger8f4b1352012-05-15 08:59:06 +000030#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000031
Stephen Warren69e1e352014-07-25 17:30:48 -060032/*
33 * The timeout for the initial BOOTP/DHCP request used to be described by a
Tom Rini26011a32022-03-11 09:12:02 -050034 * counter of fixed-length timeout periods. CONFIG_NET_RETRY_COUNT represents
Stephen Warren69e1e352014-07-25 17:30:48 -060035 * that counter
36 *
37 * Now that the timeout periods are variable (exponential backoff and retry)
38 * we convert the timeout count to the absolute time it would have take to
39 * execute that many retries, and keep sending retry packets until that time
40 * is reached.
41 */
Tom Rini26011a32022-03-11 09:12:02 -050042#define TIMEOUT_MS ((3 + (CONFIG_NET_RETRY_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000043
Sean Edmond0c962d42024-05-08 19:39:02 -070044/*
45 * According to rfc951 : 7.2. Client Retransmission Strategy
46 * "After the 'average' backoff reaches about 60 seconds, it should be
47 * increased no further, but still randomized."
48 *
49 * U-Boot has saturated this backoff at 2 seconds for a long time.
50 * To modify, set the environment variable "bootpretransmitperiodmax"
51 */
52#define RETRANSMIT_PERIOD_MAX_MS 60000
53
54/* Retransmission timeout for the initial packet (in milliseconds).
55 * This timeout will double on each retry. To modify, set the
56 * environment variable bootpretransmitperiodinit.
57 */
58#define RETRANSMIT_PERIOD_INIT_MS 250
59
Tom Rini364d0022023-01-10 11:19:45 -050060#ifndef CFG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
61#define CFG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000062#endif
63
Tom Rini364d0022023-01-10 11:19:45 -050064#ifndef CFG_BOOTP_ID_CACHE_SIZE
65#define CFG_BOOTP_ID_CACHE_SIZE 4
Thierry Reding8977cda2014-08-19 10:21:24 +020066#endif
67
Tom Rini364d0022023-01-10 11:19:45 -050068u32 bootp_ids[CFG_BOOTP_ID_CACHE_SIZE];
Thierry Reding8977cda2014-08-19 10:21:24 +020069unsigned int bootp_num_ids;
Joe Hershberger2fe81a52015-04-08 01:41:09 -050070int bootp_try;
Sean Edmond0c962d42024-05-08 19:39:02 -070071u32 bootp_id;
Stephen Warren69e1e352014-07-25 17:30:48 -060072ulong bootp_start;
73ulong bootp_timeout;
Joe Hershberger6d236432015-04-08 01:41:03 -050074char net_nis_domain[32] = {0,}; /* Our NIS domain */
75char net_hostname[32] = {0,}; /* Our hostname */
Andre Kalbe9926e52022-01-28 09:40:32 +010076char net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN] = {0,}; /* Our bootpath */
wdenk3861aa52002-09-27 23:19:37 +000077
Alexandre Messier15971322016-02-01 17:08:57 -050078static ulong time_taken_max;
Sean Edmond0c962d42024-05-08 19:39:02 -070079static u32 retransmit_period_max_ms;
Alexandre Messier15971322016-02-01 17:08:57 -050080
Jon Loeliger54f35c22007-07-09 17:45:14 -050081#if defined(CONFIG_CMD_DHCP)
Kim Phillips40c2c032012-10-29 13:34:33 +000082static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -050083static u32 dhcp_leasetime;
Joe Hershberger5874dec2015-04-08 01:41:01 -050084static struct in_addr dhcp_server_ip;
Stefan Brüns345cb832015-09-04 00:31:48 +020085static u8 dhcp_option_overload;
86#define OVERLOAD_FILE 1
87#define OVERLOAD_SNAME 2
Joe Hershberger5874dec2015-04-08 01:41:01 -050088static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
89 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000090
91/* For Debug */
wdenkb02744a2003-04-05 00:53:31 +000092#if 0
93static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000094{
95 switch (type) {
wdenk29e7f5a2004-03-12 00:14:09 +000096 case 1: return "DHCPDISCOVER"; break;
97 case 2: return "DHCPOFFER"; break;
98 case 3: return "DHCPREQUEST"; break;
99 case 4: return "DHCPDECLINE"; break;
100 case 5: return "DHCPACK"; break;
101 case 6: return "DHCPNACK"; break;
102 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +0000103 default: return "UNKNOWN/INVALID MSG TYPE"; break;
104 }
105}
wdenkb02744a2003-04-05 00:53:31 +0000106#endif
Jon Loeligera9807e52007-07-10 11:05:02 -0500107#endif
wdenk3861aa52002-09-27 23:19:37 +0000108
Thierry Reding8977cda2014-08-19 10:21:24 +0200109static void bootp_add_id(ulong id)
110{
111 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
112 size_t size = sizeof(bootp_ids) - sizeof(id);
113
114 memmove(bootp_ids, &bootp_ids[1], size);
115 bootp_ids[bootp_num_ids - 1] = id;
116 } else {
117 bootp_ids[bootp_num_ids] = id;
118 bootp_num_ids++;
119 }
120}
121
122static bool bootp_match_id(ulong id)
123{
124 unsigned int i;
125
126 for (i = 0; i < bootp_num_ids; i++)
127 if (bootp_ids[i] == id)
128 return true;
129
130 return false;
131}
132
Stefan Brünsa48cd932015-08-27 23:53:26 +0200133static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
134 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000135{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500136 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000137 int retval = 0;
138
139 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
140 retval = -1;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500141 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000142 retval = -2;
Stefan Brünsa48cd932015-08-27 23:53:26 +0200143 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000144 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000145 else if (bp->bp_htype != HWT_ETHER)
146 retval = -4;
147 else if (bp->bp_hlen != HWL_ETHER)
148 retval = -5;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500149 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000150 retval = -6;
Anton Perssonbf27a4c2016-03-17 09:38:21 +0100151 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
152 retval = -7;
wdenk3861aa52002-09-27 23:19:37 +0000153
Robin Getz9e0a4d62009-07-23 03:01:03 -0400154 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000155
156 return retval;
157}
158
Lyle Franklin73fcbc72019-08-05 06:23:42 -0400159static void store_bootp_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000160{
Joe Hershberger5874dec2015-04-08 01:41:01 -0500161 struct in_addr tmp_ip;
Alexander Graf427e6952018-06-15 10:29:28 +0200162 bool overwrite_serverip = true;
163
Simon Glass297fc202021-12-18 11:27:52 -0700164 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP))
165 return;
166
Alexander Graf427e6952018-06-15 10:29:28 +0200167#if defined(CONFIG_BOOTP_PREFER_SERVERIP)
168 overwrite_serverip = false;
169#endif
Joe Hershberger9d37a582012-05-23 07:59:18 +0000170
Joe Hershberger5874dec2015-04-08 01:41:01 -0500171 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
Alexander Graf427e6952018-06-15 10:29:28 +0200172 if (tmp_ip.s_addr != 0 && (overwrite_serverip || !net_server_ip.s_addr))
Joe Hershberger5874dec2015-04-08 01:41:01 -0500173 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500174 memcpy(net_server_ethaddr,
175 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
Stefan Brüns345cb832015-09-04 00:31:48 +0200176 if (
177#if defined(CONFIG_CMD_DHCP)
178 !(dhcp_option_overload & OVERLOAD_FILE) &&
179#endif
Alexander Graff43bf5d2018-06-15 10:29:27 +0200180 (strlen(bp->bp_file) > 0) &&
181 !net_boot_file_name_explicit) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500182 copy_filename(net_boot_file_name, bp->bp_file,
183 sizeof(net_boot_file_name));
Stefan Brüns345cb832015-09-04 00:31:48 +0200184 }
wdenk3861aa52002-09-27 23:19:37 +0000185
Joe Hershberger290c8992015-04-08 01:41:02 -0500186 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000187
188 /* Propagate to environment:
wdenk57b2d802003-06-27 21:31:46 +0000189 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000190 * not contain a new value
191 */
Joe Hershberger290c8992015-04-08 01:41:02 -0500192 if (*net_boot_file_name)
Simon Glass6a38e412017-08-03 12:22:09 -0600193 env_set("bootfile", net_boot_file_name);
Lyle Franklin73fcbc72019-08-05 06:23:42 -0400194}
195
196/*
197 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
198 */
199static void store_net_params(struct bootp_hdr *bp)
200{
201#if !defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
202 store_bootp_params(bp);
203#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500204 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000205}
206
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000207static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000208{
209 if (curlen >= maxlen) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000210 printf("*** WARNING: %s is too long (%d - max: %d)"
211 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000212 curlen = maxlen - 1;
213 }
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000214 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000215}
216
Jon Loeliger54f35c22007-07-09 17:45:14 -0500217#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000218
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500219static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000220{
wdenk29e7f5a2004-03-12 00:14:09 +0000221 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000222
Robin Getz9e0a4d62009-07-23 03:01:03 -0400223 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500224 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000225
Joe Hershberger290c8992015-04-08 01:41:02 -0500226 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000227
wdenk29e7f5a2004-03-12 00:14:09 +0000228 switch (*ext) {
229 /* Fixed length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000230 case 1: /* Subnet mask */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500231 if (net_netmask.s_addr == 0)
232 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000233 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000234 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000235 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000236 /* Variable length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000237 case 3: /* Gateways list */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500238 if (net_gateway.s_addr == 0)
239 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000240 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000241 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000242 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000243 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000244 break;
245 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500246 if (net_dns_server.s_addr == 0)
247 net_copy_ip(&net_dns_server,
248 (struct in_addr *)(ext + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500249#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500250 if ((net_dns_server2.s_addr == 0) && (size > 4))
251 net_copy_ip(&net_dns_server2,
252 (struct in_addr *)(ext + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000253#endif
wdenk3861aa52002-09-27 23:19:37 +0000254 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000255 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000256 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000257 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000258 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000259 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000260 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000261 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000262 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000263 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000264 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000265 case 12: /* Host name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500266 if (net_hostname[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000267 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500268 sizeof(net_hostname), size);
269 memcpy(&net_hostname, ext + 2, size);
270 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000271 }
272 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000273 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000274 if (size == 2)
Joe Hershberger290c8992015-04-08 01:41:02 -0500275 net_boot_file_expected_size_in_blocks =
276 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000277 else if (size == 4)
Joe Hershberger290c8992015-04-08 01:41:02 -0500278 net_boot_file_expected_size_in_blocks =
279 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000280 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000281 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000282 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000283 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000284 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000285 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000286 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000287 case 17: /* Root path */
Joe Hershberger6d236432015-04-08 01:41:03 -0500288 if (net_root_path[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000289 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500290 sizeof(net_root_path), size);
291 memcpy(&net_root_path, ext + 2, size);
292 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000293 }
294 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000295 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000296 /*
wdenk57b2d802003-06-27 21:31:46 +0000297 * This can be used to send the information of the
298 * vendor area in another file that the client can
299 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000300 */
301 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000302 /* IP host layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000303 case 40: /* NIS Domain name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500304 if (net_nis_domain[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000305 size = truncate_sz("NIS Domain Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500306 sizeof(net_nis_domain), size);
307 memcpy(&net_nis_domain, ext + 2, size);
308 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000309 }
310 break;
Luuk Paulussen6380e012011-05-16 18:29:19 +0000311#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
312 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500313 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen6380e012011-05-16 18:29:19 +0000314 break;
315#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000316 /* Application layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000317 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000318 /*
wdenk57b2d802003-06-27 21:31:46 +0000319 * Binary information to exchange specific
320 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000321 */
322 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000323 /* Reserved (custom) fields (128..254) */
324 }
wdenk3861aa52002-09-27 23:19:37 +0000325}
326
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500327static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000328{
wdenk29e7f5a2004-03-12 00:14:09 +0000329 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000330
Robin Getz9e0a4d62009-07-23 03:01:03 -0400331 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000332
wdenk29e7f5a2004-03-12 00:14:09 +0000333 while ((ext < end) && (*ext != 0xff)) {
334 if (*ext == 0) {
335 ext++;
336 } else {
337 u8 *opt = ext;
338
339 ext += ext[1] + 2;
340 if (ext <= end)
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500341 bootp_process_vendor_field(opt);
wdenk29e7f5a2004-03-12 00:14:09 +0000342 }
wdenk3861aa52002-09-27 23:19:37 +0000343 }
wdenk3861aa52002-09-27 23:19:37 +0000344
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000345 debug("[BOOTP] Received fields:\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500346 if (net_netmask.s_addr)
347 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000348
Joe Hershberger5874dec2015-04-08 01:41:01 -0500349 if (net_gateway.s_addr)
350 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000351
Joe Hershberger290c8992015-04-08 01:41:02 -0500352 if (net_boot_file_expected_size_in_blocks)
353 debug("net_boot_file_expected_size_in_blocks : %d\n",
354 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000355
Joe Hershberger6d236432015-04-08 01:41:03 -0500356 if (net_hostname[0])
357 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000358
Joe Hershberger6d236432015-04-08 01:41:03 -0500359 if (net_root_path[0])
360 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000361
Joe Hershberger6d236432015-04-08 01:41:03 -0500362 if (net_nis_domain[0])
363 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000364
Luuk Paulussen6380e012011-05-16 18:29:19 +0000365#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Chris Packham3a2ae812018-05-03 20:19:03 +1200366 if (net_ntp_server.s_addr)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500367 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen6380e012011-05-16 18:29:19 +0000368#endif
wdenk3861aa52002-09-27 23:19:37 +0000369}
Simon Glassab068eb2011-06-13 16:13:12 -0700370
wdenk3861aa52002-09-27 23:19:37 +0000371/*
372 * Handle a BOOTP received packet.
373 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500374static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
375 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000376{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500377 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000378
Robin Getz9e0a4d62009-07-23 03:01:03 -0400379 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500380 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000381
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500382 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000383
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000384 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +0200385 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000386 return;
387
388 /*
wdenk29e7f5a2004-03-12 00:14:09 +0000389 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000390 */
Uri Mashiach4892d392017-01-19 10:51:45 +0200391#if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
392 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000393#endif
394
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500395 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000396
397 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500398 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500399 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000400
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500401 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +0000402 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000403
Robin Getz9e0a4d62009-07-23 03:01:03 -0400404 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000405
Simon Glass5234ad12011-10-27 06:24:32 +0000406 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000407}
Jon Loeligera9807e52007-07-10 11:05:02 -0500408#endif
wdenk3861aa52002-09-27 23:19:37 +0000409
410/*
411 * Timeout on BOOTP/DHCP request.
412 */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500413static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000414{
Stephen Warren69e1e352014-07-25 17:30:48 -0600415 ulong time_taken = get_timer(bootp_start);
Sean Edmond0c962d42024-05-08 19:39:02 -0700416 int rand_minus_plus_100;
Stephen Warren69e1e352014-07-25 17:30:48 -0600417
Alexandre Messier15971322016-02-01 17:08:57 -0500418 if (time_taken >= time_taken_max) {
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000419#ifdef CONFIG_BOOTP_MAY_FAIL
Joe Hershberger724cc6a2017-11-07 18:13:40 -0800420 char *ethrotate;
421
422 ethrotate = env_get("ethrotate");
423 if ((ethrotate && strcmp(ethrotate, "no") == 0) ||
424 net_restart_wrap) {
425 puts("\nRetry time exceeded\n");
426 net_set_state(NETLOOP_FAIL);
427 } else
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000428#endif
Joe Hershberger724cc6a2017-11-07 18:13:40 -0800429 {
430 puts("\nRetry time exceeded; starting again\n");
431 net_start_again();
432 }
wdenk3861aa52002-09-27 23:19:37 +0000433 } else {
Stephen Warren69e1e352014-07-25 17:30:48 -0600434 bootp_timeout *= 2;
Sean Edmond0c962d42024-05-08 19:39:02 -0700435 if (bootp_timeout > retransmit_period_max_ms)
436 bootp_timeout = retransmit_period_max_ms;
437
438 /* Randomize by adding bootp_timeout*RAND, where RAND
439 * is a randomization factor between -0.1..+0.1
440 */
441 srand(get_ticks() + rand());
442 rand_minus_plus_100 = ((rand() % 200) - 100);
443 bootp_timeout = bootp_timeout +
444 (((int)bootp_timeout * rand_minus_plus_100) / 1000);
445
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500446 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500447 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000448 }
449}
450
Ilya Yanok30876582012-09-17 10:26:25 +0000451#define put_vci(e, str) \
452 do { \
453 size_t vci_strlen = strlen(str); \
454 *e++ = 60; /* Vendor Class Identifier */ \
455 *e++ = vci_strlen; \
456 memcpy(e, str, vci_strlen); \
457 e += vci_strlen; \
458 } while (0)
459
Alexander Grafea73a042016-05-06 21:01:02 +0200460static u8 *add_vci(u8 *e)
461{
Alexander Graf3661aac2016-05-06 21:01:07 +0200462 char *vci = NULL;
Simon Glass64b723f2017-08-03 12:22:12 -0600463 char *env_vci = env_get("bootp_vci");
Alexander Graf3661aac2016-05-06 21:01:07 +0200464
Simon Glass0e84d962024-09-29 19:49:50 -0600465#if defined(CONFIG_XPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
Alexander Graf3661aac2016-05-06 21:01:07 +0200466 vci = CONFIG_SPL_NET_VCI_STRING;
Alexander Grafea73a042016-05-06 21:01:02 +0200467#elif defined(CONFIG_BOOTP_VCI_STRING)
Alexander Graf3661aac2016-05-06 21:01:07 +0200468 vci = CONFIG_BOOTP_VCI_STRING;
Alexander Grafea73a042016-05-06 21:01:02 +0200469#endif
470
Alexander Graf3661aac2016-05-06 21:01:07 +0200471 if (env_vci)
472 vci = env_vci;
473
474 if (vci)
475 put_vci(e, vci);
476
Alexander Grafea73a042016-05-06 21:01:02 +0200477 return e;
478}
479
wdenk3861aa52002-09-27 23:19:37 +0000480/*
481 * Initialize BOOTP extension fields in the request.
482 */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500483#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500484static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
485 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000486{
wdenk29e7f5a2004-03-12 00:14:09 +0000487 u8 *start = e;
488 u8 *cnt;
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200489#ifdef CONFIG_LIB_UUID
Jason Hobbsea3202812011-08-31 05:37:31 +0000490 char *uuid;
Jason Hobbsea3202812011-08-31 05:37:31 +0000491#endif
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200492 int clientarch = -1;
wdenk29e7f5a2004-03-12 00:14:09 +0000493
Jon Loeliger5336a762007-07-09 22:08:34 -0500494#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000495 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000496#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500497#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200498 char *hostname;
stroesee0aadfb2003-08-28 14:17:32 +0000499#endif
wdenk3861aa52002-09-27 23:19:37 +0000500
wdenk29e7f5a2004-03-12 00:14:09 +0000501 *e++ = 99; /* RFC1048 Magic Cookie */
502 *e++ = 130;
503 *e++ = 83;
504 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000505
wdenk29e7f5a2004-03-12 00:14:09 +0000506 *e++ = 53; /* DHCP Message Type */
507 *e++ = 1;
508 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000509
wdenk29e7f5a2004-03-12 00:14:09 +0000510 *e++ = 57; /* Maximum DHCP Message Size */
511 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000512 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
513 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000514
Joe Hershberger5874dec2015-04-08 01:41:01 -0500515 if (server_ip.s_addr) {
516 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000517
wdenk29e7f5a2004-03-12 00:14:09 +0000518 *e++ = 54; /* ServerID */
519 *e++ = 4;
520 *e++ = tmp >> 24;
521 *e++ = tmp >> 16;
522 *e++ = tmp >> 8;
523 *e++ = tmp & 0xff;
524 }
wdenk3861aa52002-09-27 23:19:37 +0000525
Joe Hershberger5874dec2015-04-08 01:41:01 -0500526 if (requested_ip.s_addr) {
527 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000528
wdenk29e7f5a2004-03-12 00:14:09 +0000529 *e++ = 50; /* Requested IP */
530 *e++ = 4;
531 *e++ = tmp >> 24;
532 *e++ = tmp >> 16;
533 *e++ = tmp >> 8;
534 *e++ = tmp & 0xff;
535 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500536#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Simon Glass64b723f2017-08-03 12:22:12 -0600537 hostname = env_get("hostname");
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000538 if (hostname) {
539 int hostnamelen = strlen(hostname);
wdenk29e7f5a2004-03-12 00:14:09 +0000540
541 *e++ = 12; /* Hostname */
542 *e++ = hostnamelen;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000543 memcpy(e, hostname, hostnamelen);
wdenk29e7f5a2004-03-12 00:14:09 +0000544 e += hostnamelen;
545 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000546#endif
547
Heinrich Schuchardtc0c4df72025-06-08 09:42:28 +0200548#ifdef CONFIG_DHCP_PXE_CLIENTARCH
549 clientarch = CONFIG_DHCP_PXE_CLIENTARCH;
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200550#endif
551
Simon Glass64b723f2017-08-03 12:22:12 -0600552 if (env_get("bootp_arch"))
Simon Glass22c34c22017-08-03 12:22:13 -0600553 clientarch = env_get_ulong("bootp_arch", 16, clientarch);
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200554
Heinrich Schuchardtc0c4df72025-06-08 09:42:28 +0200555 if (clientarch != 0xff) {
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200556 *e++ = 93; /* Client System Architecture */
557 *e++ = 2;
558 *e++ = (clientarch >> 8) & 0xff;
559 *e++ = clientarch & 0xff;
560 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000561
562 *e++ = 94; /* Client Network Interface Identifier */
563 *e++ = 3;
564 *e++ = 1; /* type field for UNDI */
565 *e++ = 0; /* major revision */
566 *e++ = 0; /* minor revision */
567
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200568#ifdef CONFIG_LIB_UUID
Simon Glass64b723f2017-08-03 12:22:12 -0600569 uuid = env_get("pxeuuid");
Jason Hobbsea3202812011-08-31 05:37:31 +0000570
571 if (uuid) {
572 if (uuid_str_valid(uuid)) {
573 *e++ = 97; /* Client Machine Identifier */
574 *e++ = 17;
575 *e++ = 0; /* type 0 - UUID */
576
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200577 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsea3202812011-08-31 05:37:31 +0000578 e += 16;
579 } else {
580 printf("Invalid pxeuuid: %s\n", uuid);
581 }
582 }
Ilya Yanok30876582012-09-17 10:26:25 +0000583#endif
Jason Hobbsea3202812011-08-31 05:37:31 +0000584
Alexander Grafea73a042016-05-06 21:01:02 +0200585 e = add_vci(e);
stroesee0aadfb2003-08-28 14:17:32 +0000586
Jon Loeliger5336a762007-07-09 22:08:34 -0500587#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000588 x = dhcp_vendorex_prep(e);
589 if (x)
wdenk29e7f5a2004-03-12 00:14:09 +0000590 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000591#endif
592
wdenk29e7f5a2004-03-12 00:14:09 +0000593 *e++ = 55; /* Parameter Request List */
594 cnt = e++; /* Pointer to count of requested items */
595 *cnt = 0;
Jon Loeliger5336a762007-07-09 22:08:34 -0500596#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000597 *e++ = 1; /* Subnet Mask */
598 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000599#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500600#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000601 *e++ = 2;
602 *cnt += 1;
603#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500604#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000605 *e++ = 3; /* Router Option */
606 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000607#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500608#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000609 *e++ = 6; /* DNS Server(s) */
610 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000611#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500612#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000613 *e++ = 12; /* Hostname */
614 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000615#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500616#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000617 *e++ = 13; /* Boot File Size */
618 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000619#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500620#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000621 *e++ = 17; /* Boot path */
622 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000623#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500624#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000625 *e++ = 40; /* NIS Domain name request */
626 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000627#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500628#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000629 *e++ = 42;
630 *cnt += 1;
631#endif
Sean Edmond57867112023-07-25 16:20:30 -0700632 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION)) {
Sean Edmond0760efe2024-05-08 19:39:01 -0700633 *e++ = DHCP_OPTION_PXE_CONFIG_FILE; /* PXELINUX Config File */
Sean Edmond57867112023-07-25 16:20:30 -0700634 *cnt += 1;
635 }
Jason Liu1c7dca52010-11-14 12:23:09 +0800636 /* no options, so back up to avoid sending an empty request list */
637 if (*cnt == 0)
638 e -= 2;
639
wdenk29e7f5a2004-03-12 00:14:09 +0000640 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000641
wdenk29e7f5a2004-03-12 00:14:09 +0000642 /* Pad to minimal length */
Tom Rini364d0022023-01-10 11:19:45 -0500643#ifdef CFG_DHCP_MIN_EXT_LEN
644 while ((e - start) < CFG_DHCP_MIN_EXT_LEN)
wdenk29e7f5a2004-03-12 00:14:09 +0000645 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000646#endif
647
wdenk29e7f5a2004-03-12 00:14:09 +0000648 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000649}
650
Jon Loeligera9807e52007-07-10 11:05:02 -0500651#else
wdenk3861aa52002-09-27 23:19:37 +0000652/*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000653 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000654 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500655static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000656{
wdenk29e7f5a2004-03-12 00:14:09 +0000657 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000658
wdenk29e7f5a2004-03-12 00:14:09 +0000659 *e++ = 99; /* RFC1048 Magic Cookie */
660 *e++ = 130;
661 *e++ = 83;
662 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000663
Jon Loeliger54f35c22007-07-09 17:45:14 -0500664#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000665 *e++ = 53; /* DHCP Message Type */
666 *e++ = 1;
667 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000668
wdenk29e7f5a2004-03-12 00:14:09 +0000669 *e++ = 57; /* Maximum DHCP Message Size */
670 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000671 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
672 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeligera9807e52007-07-10 11:05:02 -0500673#endif
wdenk3861aa52002-09-27 23:19:37 +0000674
Walter Stoll8182a3a2021-10-12 11:01:59 +0000675 e = add_vci(e);
Ilya Yanok30876582012-09-17 10:26:25 +0000676
Jon Loeliger5336a762007-07-09 22:08:34 -0500677#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000678 *e++ = 1; /* Subnet mask request */
679 *e++ = 4;
680 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000681#endif
682
Jon Loeliger5336a762007-07-09 22:08:34 -0500683#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000684 *e++ = 3; /* Default gateway request */
685 *e++ = 4;
686 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000687#endif
688
Jon Loeliger5336a762007-07-09 22:08:34 -0500689#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000690 *e++ = 6; /* Domain Name Server */
691 *e++ = 4;
692 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000693#endif
694
Jon Loeliger5336a762007-07-09 22:08:34 -0500695#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000696 *e++ = 12; /* Host name request */
697 *e++ = 32;
698 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000699#endif
700
Jon Loeliger5336a762007-07-09 22:08:34 -0500701#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000702 *e++ = 13; /* Boot file size */
703 *e++ = 2;
704 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000705#endif
706
Jon Loeliger5336a762007-07-09 22:08:34 -0500707#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000708 *e++ = 17; /* Boot path */
709 *e++ = 32;
710 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000711#endif
712
Jon Loeliger5336a762007-07-09 22:08:34 -0500713#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000714 *e++ = 40; /* NIS Domain name request */
715 *e++ = 32;
716 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000717#endif
Luuk Paulussen6380e012011-05-16 18:29:19 +0000718#if defined(CONFIG_BOOTP_NTPSERVER)
719 *e++ = 42;
720 *e++ = 4;
721 e += 4;
722#endif
wdenk3861aa52002-09-27 23:19:37 +0000723
wdenk29e7f5a2004-03-12 00:14:09 +0000724 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000725
Andre Renaud23298cd2016-05-05 07:28:08 -0600726 /*
727 * If nothing in list, remove it altogether. Some DHCP servers get
728 * upset by this minor faux pas and do not respond at all.
729 */
730 if (e == start + 3) {
731 printf("*** Warning: no DHCP options requested\n");
732 e -= 3;
733 }
734
wdenk29e7f5a2004-03-12 00:14:09 +0000735 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000736}
Jon Loeligera9807e52007-07-10 11:05:02 -0500737#endif
wdenk3861aa52002-09-27 23:19:37 +0000738
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500739void bootp_reset(void)
Stephen Warren69e1e352014-07-25 17:30:48 -0600740{
Thierry Reding8977cda2014-08-19 10:21:24 +0200741 bootp_num_ids = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500742 bootp_try = 0;
Stephen Warren69e1e352014-07-25 17:30:48 -0600743 bootp_start = get_timer(0);
Sean Edmond0c962d42024-05-08 19:39:02 -0700744
745 bootp_timeout = env_get_ulong("bootpretransmitperiodinit", 10, RETRANSMIT_PERIOD_INIT_MS);
Stephen Warren69e1e352014-07-25 17:30:48 -0600746}
747
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500748void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000749{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000750 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500751 struct bootp_hdr *bp;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000752 int extlen, pktlen, iplen;
753 int eth_hdr_size;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000754#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek764c29d2014-07-11 11:39:37 +0200755 ulong rand_ms;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000756#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500757 struct in_addr zero_ip;
758 struct in_addr bcast_ip;
Alexandre Messier15971322016-02-01 17:08:57 -0500759 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000760
Simon Glass768cbf02011-12-10 11:08:06 +0000761 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger54f35c22007-07-09 17:45:14 -0500762#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000763 dhcp_state = INIT;
764#endif
765
Simon Glass64b723f2017-08-03 12:22:12 -0600766 ep = env_get("bootpretryperiod");
Alexandre Messier15971322016-02-01 17:08:57 -0500767 if (ep != NULL)
Simon Glassff9b9032021-07-24 09:03:30 -0600768 time_taken_max = dectoul(ep, NULL);
Alexandre Messier15971322016-02-01 17:08:57 -0500769 else
770 time_taken_max = TIMEOUT_MS;
771
Sean Edmond0c962d42024-05-08 19:39:02 -0700772 retransmit_period_max_ms = env_get_ulong("bootpretransmitperiodmax", 10,
773 RETRANSMIT_PERIOD_MAX_MS);
774
wdenk3861aa52002-09-27 23:19:37 +0000775#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500776 if (bootp_try == 0)
Joe Hershberger797f2c52012-05-23 07:57:58 +0000777 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000778
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500779 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
780 rand_ms = rand() >> (22 - bootp_try);
Joe Hershberger797f2c52012-05-23 07:57:58 +0000781 else /* After 3rd BOOTP request max 8192 * 1ms */
782 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000783
Joe Hershberger797f2c52012-05-23 07:57:58 +0000784 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek764c29d2014-07-11 11:39:37 +0200785 mdelay(rand_ms);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000786
wdenk3861aa52002-09-27 23:19:37 +0000787#endif /* CONFIG_BOOTP_RANDOM_DELAY */
788
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500789 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500790 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000791 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000792
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500793 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000794 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000795
796 /*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000797 * Next line results in incorrect packet size being transmitted,
798 * resulting in errors in some DHCP servers, reporting missing bytes.
799 * Size must be set in packet header after extension length has been
800 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000801 * C. Hallinan, DS4.COM, Inc.
802 */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000803 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500804 sizeof (struct bootp_hdr)); */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000805 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000806 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000807
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500808 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000809 bp->bp_op = OP_BOOTREQUEST;
810 bp->bp_htype = HWT_ETHER;
811 bp->bp_hlen = HWL_ETHER;
812 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +0200813 /*
814 * according to RFC1542, should be 0 on first request, secs since
815 * first request otherwise
816 */
817 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500818 zero_ip.s_addr = 0;
819 net_write_ip(&bp->bp_ciaddr, zero_ip);
820 net_write_ip(&bp->bp_yiaddr, zero_ip);
821 net_write_ip(&bp->bp_siaddr, zero_ip);
822 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500823 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger290c8992015-04-08 01:41:02 -0500824 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000825
826 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500827#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500828 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
829 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000830#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500831 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeligera9807e52007-07-10 11:05:02 -0500832#endif
wdenk3861aa52002-09-27 23:19:37 +0000833
Sean Edmond0c962d42024-05-08 19:39:02 -0700834 /* Only generate a new transaction ID for each new BOOTP request */
835 if (bootp_try == 1) {
Sean Edmondd121e352024-05-08 19:39:03 -0700836 if (IS_ENABLED(CONFIG_BOOTP_RANDOM_XID)) {
837 srand(get_ticks() + rand());
838 bootp_id = rand();
839 } else {
840 /*
841 * Bootp ID is the lower 4 bytes of our ethernet address
842 * plus the current time in ms.
843 */
844 bootp_id = ((u32)net_ethaddr[2] << 24)
845 | ((u32)net_ethaddr[3] << 16)
846 | ((u32)net_ethaddr[4] << 8)
847 | (u32)net_ethaddr[5];
848 bootp_id += get_timer(0);
849 bootp_id = htonl(bootp_id);
850 }
Sean Edmond0c962d42024-05-08 19:39:02 -0700851 }
wdenk3861aa52002-09-27 23:19:37 +0000852
wdenk3861aa52002-09-27 23:19:37 +0000853 bootp_add_id(bootp_id);
854 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000855 /*
856 * Calculate proper packet lengths taking into account the
857 * variable size of the options field
858 */
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000859 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
860 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500861 bcast_ip.s_addr = 0xFFFFFFFFL;
862 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500863 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000864
Jon Loeliger54f35c22007-07-09 17:45:14 -0500865#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000866 dhcp_state = SELECTING;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500867 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000868#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500869 net_set_udp_handler(bootp_handler);
Jon Loeligera9807e52007-07-10 11:05:02 -0500870#endif
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500871 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000872}
873
Jon Loeliger54f35c22007-07-09 17:45:14 -0500874#if defined(CONFIG_CMD_DHCP)
Stefan Brüns79f724c2015-09-04 00:31:49 +0200875static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000876{
wdenk3861aa52002-09-27 23:19:37 +0000877 int oplen, size;
Wolfgang Denk2b184222009-09-11 09:05:32 +0200878#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
879 int *to_ptr;
880#endif
wdenk3861aa52002-09-27 23:19:37 +0000881
wdenk29e7f5a2004-03-12 00:14:09 +0000882 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000883 oplen = *(popt + 1);
wdenk29e7f5a2004-03-12 00:14:09 +0000884 switch (*popt) {
Stefan Brüns44318292015-08-28 10:15:54 +0200885 case 0:
886 oplen = -1; /* Pad omits len byte */
887 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000888 case 1:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500889 net_copy_ip(&net_netmask, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000890 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500891#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000892 case 2: /* Time offset */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500893 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500894 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500895 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkb4ad9622005-04-01 00:25:43 +0000896 break;
897#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000898 case 3:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500899 net_copy_ip(&net_gateway, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000900 break;
901 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500902 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500903#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000904 if (*(popt + 1) > 4)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500905 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000906#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000907 break;
908 case 12:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000909 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500910 sizeof(net_hostname), oplen);
911 memcpy(&net_hostname, popt + 2, size);
912 net_hostname[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000913 break;
914 case 15: /* Ignore Domain Name Option */
915 break;
916 case 17:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000917 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500918 sizeof(net_root_path), oplen);
919 memcpy(&net_root_path, popt + 2, size);
920 net_root_path[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000921 break;
Brian Rzycki5a672a22012-09-11 09:22:53 +0000922 case 28: /* Ignore Broadcast Address Option */
923 break;
Charles Hardinac178f72024-04-12 13:45:33 -0700924 case 40: /* NIS Domain name */
925 if (net_nis_domain[0] == 0) {
926 size = truncate_sz("NIS Domain Name",
Heinrich Schuchardt92b982b2024-04-23 09:09:44 +0200927 sizeof(net_nis_domain), oplen);
Charles Hardinac178f72024-04-12 13:45:33 -0700928 memcpy(&net_nis_domain, popt + 2, size);
929 net_nis_domain[size] = 0;
930 }
931 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500932#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000933 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500934 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkb4ad9622005-04-01 00:25:43 +0000935 break;
936#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000937 case 51:
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500938 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000939 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200940 case 52:
941 dhcp_option_overload = popt[2];
942 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000943 case 53: /* Ignore Message Type Option */
944 break;
945 case 54:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500946 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000947 break;
948 case 58: /* Ignore Renewal Time Option */
949 break;
950 case 59: /* Ignore Rebinding Time Option */
951 break;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100952 case 66: /* Ignore TFTP server name */
953 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200954 case 67: /* Bootfile option */
Alexander Graff43bf5d2018-06-15 10:29:27 +0200955 if (!net_boot_file_name_explicit) {
956 size = truncate_sz("Bootfile",
957 sizeof(net_boot_file_name),
958 oplen);
959 memcpy(&net_boot_file_name, popt + 2, size);
960 net_boot_file_name[size] = 0;
961 }
Wolfgang Denk012429c2006-03-12 18:26:46 +0100962 break;
Sean Edmond0760efe2024-05-08 19:39:01 -0700963 case DHCP_OPTION_PXE_CONFIG_FILE: /* PXELINUX Config File */
Sean Edmond57867112023-07-25 16:20:30 -0700964 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION)) {
965 /* In case it has already been allocated when get DHCP Offer packet,
966 * free first to avoid memory leak.
967 */
968 if (pxelinux_configfile)
969 free(pxelinux_configfile);
970
971 pxelinux_configfile = (char *)malloc((oplen + 1) * sizeof(char));
972
973 if (pxelinux_configfile)
974 strlcpy(pxelinux_configfile, popt + 2, oplen + 1);
975 else
976 printf("Error: Failed to allocate pxelinux_configfile\n");
977 }
978 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000979 default:
Jon Loeliger5336a762007-07-09 22:08:34 -0500980#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000981 if (dhcp_vendorex_proc(popt))
wdenk57b2d802003-06-27 21:31:46 +0000982 break;
wdenk3861aa52002-09-27 23:19:37 +0000983#endif
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000984 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500985 " %d\n", *popt);
wdenk29e7f5a2004-03-12 00:14:09 +0000986 break;
wdenk3861aa52002-09-27 23:19:37 +0000987 }
988 popt += oplen + 2; /* Process next option */
989 }
990}
991
Stefan Brüns79f724c2015-09-04 00:31:49 +0200992static void dhcp_packet_process_options(struct bootp_hdr *bp)
993{
994 uchar *popt = (uchar *)&bp->bp_vend[4];
995 uchar *end = popt + BOOTP_HDR_SIZE;
996
997 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
998 return;
999
1000 dhcp_option_overload = 0;
1001
1002 /*
1003 * The 'options' field MUST be interpreted first, 'file' next,
1004 * 'sname' last.
1005 */
1006 dhcp_process_options(popt, end);
1007
1008 if (dhcp_option_overload & OVERLOAD_FILE) {
1009 popt = (uchar *)bp->bp_file;
1010 end = popt + sizeof(bp->bp_file);
1011 dhcp_process_options(popt, end);
1012 }
1013
1014 if (dhcp_option_overload & OVERLOAD_SNAME) {
1015 popt = (uchar *)bp->bp_sname;
1016 end = popt + sizeof(bp->bp_sname);
1017 dhcp_process_options(popt, end);
1018 }
1019}
1020
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001021static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +00001022{
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -05001023 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +00001024 return -1;
1025
1026 popt += 4;
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001027 while (*popt != 0xff) {
1028 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +00001029 return *(popt + 2);
Stefan Brüns44318292015-08-28 10:15:54 +02001030 if (*popt == 0) {
1031 /* Pad */
1032 popt += 1;
1033 } else {
1034 /* Scan through all options */
1035 popt += *(popt + 1) + 2;
1036 }
wdenk3861aa52002-09-27 23:19:37 +00001037 }
1038 return -1;
1039}
1040
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001041static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +00001042{
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001043 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001044 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +00001045 int pktlen, iplen, extlen;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001046 int eth_hdr_size;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001047 struct in_addr offered_ip;
1048 struct in_addr zero_ip;
1049 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +00001050
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001051 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001052 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001053 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +00001054
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001055 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001056 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +00001057
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001058 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001059 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +00001060
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001061 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001062 bp->bp_op = OP_BOOTREQUEST;
1063 bp->bp_htype = HWT_ETHER;
1064 bp->bp_hlen = HWL_ETHER;
1065 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +02001066 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001067 /* Do not set the client IP, your IP, or server IP yet, since it
1068 * hasn't been ACK'ed by the server yet */
Justin Flammia01f256b2007-10-29 17:40:35 -04001069
Wolfgang Denk8a933fb2006-10-12 00:01:08 +02001070 /*
Wolfgang Denk68e83a82006-10-09 01:26:14 +02001071 * RFC3046 requires Relay Agents to discard packets with
1072 * nonzero and offered giaddr
1073 */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001074 zero_ip.s_addr = 0;
1075 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denk68e83a82006-10-09 01:26:14 +02001076
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001077 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierad28a312016-01-28 11:19:02 -05001078 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +00001079
1080 /*
1081 * ID is the id of the OFFER packet
1082 */
1083
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -05001084 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +00001085
1086 /*
1087 * Copy options from OFFER packet if present
1088 */
Justin Flammia01f256b2007-10-29 17:40:35 -04001089
1090 /* Copy offered IP into the parameters request list */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001091 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
1092 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
1093 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +00001094
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001095 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
1096 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001097 bcast_ip.s_addr = 0xFFFFFFFFL;
1098 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +00001099
Joe Hershbergerb1e94762012-05-23 07:59:11 +00001100 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001101 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +00001102}
1103
1104/*
1105 * Handle DHCP received packets.
1106 */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001107static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1108 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +00001109{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001110 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001111
Robin Getz9e0a4d62009-07-23 03:01:03 -04001112 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001113 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001114
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001115 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +02001116 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001117 return;
1118
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001119 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1120 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001121
Lyle Franklin73fcbc72019-08-05 06:23:42 -04001122 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0) {
1123#if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1124 store_bootp_params(bp);
1125#endif
Peng Fan67e72e92016-01-07 15:28:23 +08001126 return;
Lyle Franklin73fcbc72019-08-05 06:23:42 -04001127 }
Peng Fan67e72e92016-01-07 15:28:23 +08001128
wdenk3861aa52002-09-27 23:19:37 +00001129 switch (dhcp_state) {
1130 case SELECTING:
1131 /*
1132 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001133 * to arrive. Then select one, and generate DHCPREQUEST
1134 * response. If filename is in format we recognize, assume it
1135 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001136 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001137 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001138#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001139 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001140 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001141 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001142#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Sean Anderson123d9002023-10-14 16:47:53 -04001143 if (CONFIG_IS_ENABLED(UNIT_TEST) &&
1144 dhcp_message_type((u8 *)bp->bp_vend) == -1) {
1145 debug("got BOOTP response; transitioning to BOUND\n");
1146 goto dhcp_got_bootp;
1147 }
Stefan Brüns79f724c2015-09-04 00:31:49 +02001148 dhcp_packet_process_options(bp);
Jan Kiszkaf1389822022-10-14 18:10:06 +02001149 if (CONFIG_IS_ENABLED(EFI_LOADER) &&
Simon Glass8ce7c822023-02-05 15:40:20 -07001150 IS_ENABLED(CONFIG_NETDEVICES))
Jan Kiszkaf1389822022-10-14 18:10:06 +02001151 efi_net_set_dhcp_ack(pkt, len);
wdenk3861aa52002-09-27 23:19:37 +00001152
Lyle Franklin73fcbc72019-08-05 06:23:42 -04001153#if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1154 if (!net_server_ip.s_addr)
1155 udelay(CONFIG_SERVERIP_FROM_PROXYDHCP_DELAY_MS *
1156 1000);
1157#endif /* CONFIG_SERVERIP_FROM_PROXYDHCP */
1158
Robin Getz9e0a4d62009-07-23 03:01:03 -04001159 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001160 dhcp_state = REQUESTING;
stroese5fa6e902003-04-10 13:26:44 +00001161
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001162 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001163 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001164#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001165 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001166#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001167
1168 return;
1169 break;
1170 case REQUESTING:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001171 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001172
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001173 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Sean Anderson123d9002023-10-14 16:47:53 -04001174dhcp_got_bootp:
Stefan Brüns79f724c2015-09-04 00:31:49 +02001175 dhcp_packet_process_options(bp);
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001176 /* Store net params from reply */
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001177 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001178 dhcp_state = BOUND;
Thierry Reding8977cda2014-08-19 10:21:24 +02001179 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001180 &net_ip, get_timer(bootp_start));
Stefan Brüns4c60e4a2015-08-30 17:47:17 +02001181 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +00001182 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001183 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001184
Simon Glass5234ad12011-10-27 06:24:32 +00001185 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001186 return;
1187 }
1188 break;
Remy Bohmer03a44492008-08-20 11:30:28 +02001189 case BOUND:
1190 /* DHCP client bound to address */
1191 break;
wdenk3861aa52002-09-27 23:19:37 +00001192 default:
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001193 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001194 break;
1195 }
wdenk3861aa52002-09-27 23:19:37 +00001196}
1197
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001198void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001199{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001200 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001201}
Wolfgang Denkf7a7f082007-11-03 23:09:27 +01001202#endif /* CONFIG_CMD_DHCP */