blob: c34ffe27854a95a757534a7576f667d9659adf78 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk29e7f5a2004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060012#include <bootstage.h>
wdenk3861aa52002-09-27 23:19:37 +000013#include <command.h>
Simon Glass6eaea252019-08-01 09:46:48 -060014#include <env.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020015#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
wdenk3861aa52002-09-27 23:19:37 +000017#include <net.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <rand.h>
Simon Glass6f044982020-05-10 11:39:52 -060019#include <uuid.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020021#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000022#include "bootp.h"
Uri Mashiach4892d392017-01-19 10:51:45 +020023#ifdef CONFIG_LED_STATUS
wdenk3861aa52002-09-27 23:19:37 +000024#include <status_led.h>
25#endif
Kim Phillips576175b2012-07-05 13:19:32 +000026#ifdef CONFIG_BOOTP_RANDOM_DELAY
27#include "net_rand.h"
28#endif
Sean Edmond57867112023-07-25 16:20:30 -070029#include <malloc.h>
wdenk3861aa52002-09-27 23:19:37 +000030
Joe Hershberger8f4b1352012-05-15 08:59:06 +000031#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000032
Stephen Warren69e1e352014-07-25 17:30:48 -060033/*
34 * The timeout for the initial BOOTP/DHCP request used to be described by a
Tom Rini26011a32022-03-11 09:12:02 -050035 * counter of fixed-length timeout periods. CONFIG_NET_RETRY_COUNT represents
Stephen Warren69e1e352014-07-25 17:30:48 -060036 * that counter
37 *
38 * Now that the timeout periods are variable (exponential backoff and retry)
39 * we convert the timeout count to the absolute time it would have take to
40 * execute that many retries, and keep sending retry packets until that time
41 * is reached.
42 */
Tom Rini26011a32022-03-11 09:12:02 -050043#define TIMEOUT_MS ((3 + (CONFIG_NET_RETRY_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000044
Sean Edmond0c962d42024-05-08 19:39:02 -070045/*
46 * According to rfc951 : 7.2. Client Retransmission Strategy
47 * "After the 'average' backoff reaches about 60 seconds, it should be
48 * increased no further, but still randomized."
49 *
50 * U-Boot has saturated this backoff at 2 seconds for a long time.
51 * To modify, set the environment variable "bootpretransmitperiodmax"
52 */
53#define RETRANSMIT_PERIOD_MAX_MS 60000
54
55/* Retransmission timeout for the initial packet (in milliseconds).
56 * This timeout will double on each retry. To modify, set the
57 * environment variable bootpretransmitperiodinit.
58 */
59#define RETRANSMIT_PERIOD_INIT_MS 250
60
Tom Rini364d0022023-01-10 11:19:45 -050061#ifndef CFG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
62#define CFG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000063#endif
64
Tom Rini364d0022023-01-10 11:19:45 -050065#ifndef CFG_BOOTP_ID_CACHE_SIZE
66#define CFG_BOOTP_ID_CACHE_SIZE 4
Thierry Reding8977cda2014-08-19 10:21:24 +020067#endif
68
Tom Rini364d0022023-01-10 11:19:45 -050069u32 bootp_ids[CFG_BOOTP_ID_CACHE_SIZE];
Thierry Reding8977cda2014-08-19 10:21:24 +020070unsigned int bootp_num_ids;
Joe Hershberger2fe81a52015-04-08 01:41:09 -050071int bootp_try;
Sean Edmond0c962d42024-05-08 19:39:02 -070072u32 bootp_id;
Stephen Warren69e1e352014-07-25 17:30:48 -060073ulong bootp_start;
74ulong bootp_timeout;
Joe Hershberger6d236432015-04-08 01:41:03 -050075char net_nis_domain[32] = {0,}; /* Our NIS domain */
76char net_hostname[32] = {0,}; /* Our hostname */
Andre Kalbe9926e52022-01-28 09:40:32 +010077char net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN] = {0,}; /* Our bootpath */
wdenk3861aa52002-09-27 23:19:37 +000078
Alexandre Messier15971322016-02-01 17:08:57 -050079static ulong time_taken_max;
Sean Edmond0c962d42024-05-08 19:39:02 -070080static u32 retransmit_period_max_ms;
Alexandre Messier15971322016-02-01 17:08:57 -050081
Jon Loeliger54f35c22007-07-09 17:45:14 -050082#if defined(CONFIG_CMD_DHCP)
Kim Phillips40c2c032012-10-29 13:34:33 +000083static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -050084static u32 dhcp_leasetime;
Joe Hershberger5874dec2015-04-08 01:41:01 -050085static struct in_addr dhcp_server_ip;
Stefan Brüns345cb832015-09-04 00:31:48 +020086static u8 dhcp_option_overload;
87#define OVERLOAD_FILE 1
88#define OVERLOAD_SNAME 2
Joe Hershberger5874dec2015-04-08 01:41:01 -050089static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
90 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000091
92/* For Debug */
wdenkb02744a2003-04-05 00:53:31 +000093#if 0
94static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000095{
96 switch (type) {
wdenk29e7f5a2004-03-12 00:14:09 +000097 case 1: return "DHCPDISCOVER"; break;
98 case 2: return "DHCPOFFER"; break;
99 case 3: return "DHCPREQUEST"; break;
100 case 4: return "DHCPDECLINE"; break;
101 case 5: return "DHCPACK"; break;
102 case 6: return "DHCPNACK"; break;
103 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +0000104 default: return "UNKNOWN/INVALID MSG TYPE"; break;
105 }
106}
wdenkb02744a2003-04-05 00:53:31 +0000107#endif
Jon Loeligera9807e52007-07-10 11:05:02 -0500108#endif
wdenk3861aa52002-09-27 23:19:37 +0000109
Thierry Reding8977cda2014-08-19 10:21:24 +0200110static void bootp_add_id(ulong id)
111{
112 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
113 size_t size = sizeof(bootp_ids) - sizeof(id);
114
115 memmove(bootp_ids, &bootp_ids[1], size);
116 bootp_ids[bootp_num_ids - 1] = id;
117 } else {
118 bootp_ids[bootp_num_ids] = id;
119 bootp_num_ids++;
120 }
121}
122
123static bool bootp_match_id(ulong id)
124{
125 unsigned int i;
126
127 for (i = 0; i < bootp_num_ids; i++)
128 if (bootp_ids[i] == id)
129 return true;
130
131 return false;
132}
133
Stefan Brünsa48cd932015-08-27 23:53:26 +0200134static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
135 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000136{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500137 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000138 int retval = 0;
139
140 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
141 retval = -1;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500142 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000143 retval = -2;
Stefan Brünsa48cd932015-08-27 23:53:26 +0200144 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000145 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000146 else if (bp->bp_htype != HWT_ETHER)
147 retval = -4;
148 else if (bp->bp_hlen != HWL_ETHER)
149 retval = -5;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500150 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000151 retval = -6;
Anton Perssonbf27a4c2016-03-17 09:38:21 +0100152 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
153 retval = -7;
wdenk3861aa52002-09-27 23:19:37 +0000154
Robin Getz9e0a4d62009-07-23 03:01:03 -0400155 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000156
157 return retval;
158}
159
Lyle Franklin73fcbc72019-08-05 06:23:42 -0400160static void store_bootp_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000161{
Joe Hershberger5874dec2015-04-08 01:41:01 -0500162 struct in_addr tmp_ip;
Alexander Graf427e6952018-06-15 10:29:28 +0200163 bool overwrite_serverip = true;
164
Simon Glass297fc202021-12-18 11:27:52 -0700165 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP))
166 return;
167
Alexander Graf427e6952018-06-15 10:29:28 +0200168#if defined(CONFIG_BOOTP_PREFER_SERVERIP)
169 overwrite_serverip = false;
170#endif
Joe Hershberger9d37a582012-05-23 07:59:18 +0000171
Joe Hershberger5874dec2015-04-08 01:41:01 -0500172 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
Alexander Graf427e6952018-06-15 10:29:28 +0200173 if (tmp_ip.s_addr != 0 && (overwrite_serverip || !net_server_ip.s_addr))
Joe Hershberger5874dec2015-04-08 01:41:01 -0500174 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500175 memcpy(net_server_ethaddr,
176 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
Stefan Brüns345cb832015-09-04 00:31:48 +0200177 if (
178#if defined(CONFIG_CMD_DHCP)
179 !(dhcp_option_overload & OVERLOAD_FILE) &&
180#endif
Alexander Graff43bf5d2018-06-15 10:29:27 +0200181 (strlen(bp->bp_file) > 0) &&
182 !net_boot_file_name_explicit) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500183 copy_filename(net_boot_file_name, bp->bp_file,
184 sizeof(net_boot_file_name));
Stefan Brüns345cb832015-09-04 00:31:48 +0200185 }
wdenk3861aa52002-09-27 23:19:37 +0000186
Joe Hershberger290c8992015-04-08 01:41:02 -0500187 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000188
189 /* Propagate to environment:
wdenk57b2d802003-06-27 21:31:46 +0000190 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000191 * not contain a new value
192 */
Joe Hershberger290c8992015-04-08 01:41:02 -0500193 if (*net_boot_file_name)
Simon Glass6a38e412017-08-03 12:22:09 -0600194 env_set("bootfile", net_boot_file_name);
Lyle Franklin73fcbc72019-08-05 06:23:42 -0400195}
196
197/*
198 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
199 */
200static void store_net_params(struct bootp_hdr *bp)
201{
202#if !defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
203 store_bootp_params(bp);
204#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500205 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000206}
207
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000208static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000209{
210 if (curlen >= maxlen) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000211 printf("*** WARNING: %s is too long (%d - max: %d)"
212 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000213 curlen = maxlen - 1;
214 }
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000215 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000216}
217
Jon Loeliger54f35c22007-07-09 17:45:14 -0500218#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000219
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500220static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000221{
wdenk29e7f5a2004-03-12 00:14:09 +0000222 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000223
Robin Getz9e0a4d62009-07-23 03:01:03 -0400224 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500225 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000226
Joe Hershberger290c8992015-04-08 01:41:02 -0500227 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000228
wdenk29e7f5a2004-03-12 00:14:09 +0000229 switch (*ext) {
230 /* Fixed length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000231 case 1: /* Subnet mask */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500232 if (net_netmask.s_addr == 0)
233 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000234 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000235 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000236 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000237 /* Variable length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000238 case 3: /* Gateways list */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500239 if (net_gateway.s_addr == 0)
240 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000241 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000242 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000243 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000244 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000245 break;
246 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500247 if (net_dns_server.s_addr == 0)
248 net_copy_ip(&net_dns_server,
249 (struct in_addr *)(ext + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500250#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500251 if ((net_dns_server2.s_addr == 0) && (size > 4))
252 net_copy_ip(&net_dns_server2,
253 (struct in_addr *)(ext + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000254#endif
wdenk3861aa52002-09-27 23:19:37 +0000255 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000256 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000257 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000258 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000259 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000260 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000261 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000262 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000263 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000264 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000265 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000266 case 12: /* Host name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500267 if (net_hostname[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000268 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500269 sizeof(net_hostname), size);
270 memcpy(&net_hostname, ext + 2, size);
271 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000272 }
273 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000274 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000275 if (size == 2)
Joe Hershberger290c8992015-04-08 01:41:02 -0500276 net_boot_file_expected_size_in_blocks =
277 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000278 else if (size == 4)
Joe Hershberger290c8992015-04-08 01:41:02 -0500279 net_boot_file_expected_size_in_blocks =
280 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000281 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000282 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000283 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000284 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000285 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000286 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000287 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000288 case 17: /* Root path */
Joe Hershberger6d236432015-04-08 01:41:03 -0500289 if (net_root_path[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000290 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500291 sizeof(net_root_path), size);
292 memcpy(&net_root_path, ext + 2, size);
293 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000294 }
295 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000296 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000297 /*
wdenk57b2d802003-06-27 21:31:46 +0000298 * This can be used to send the information of the
299 * vendor area in another file that the client can
300 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000301 */
302 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000303 /* IP host layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000304 case 40: /* NIS Domain name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500305 if (net_nis_domain[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000306 size = truncate_sz("NIS Domain Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500307 sizeof(net_nis_domain), size);
308 memcpy(&net_nis_domain, ext + 2, size);
309 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000310 }
311 break;
Luuk Paulussen6380e012011-05-16 18:29:19 +0000312#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
313 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500314 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen6380e012011-05-16 18:29:19 +0000315 break;
316#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000317 /* Application layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000318 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000319 /*
wdenk57b2d802003-06-27 21:31:46 +0000320 * Binary information to exchange specific
321 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000322 */
323 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000324 /* Reserved (custom) fields (128..254) */
325 }
wdenk3861aa52002-09-27 23:19:37 +0000326}
327
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500328static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000329{
wdenk29e7f5a2004-03-12 00:14:09 +0000330 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000331
Robin Getz9e0a4d62009-07-23 03:01:03 -0400332 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000333
wdenk29e7f5a2004-03-12 00:14:09 +0000334 while ((ext < end) && (*ext != 0xff)) {
335 if (*ext == 0) {
336 ext++;
337 } else {
338 u8 *opt = ext;
339
340 ext += ext[1] + 2;
341 if (ext <= end)
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500342 bootp_process_vendor_field(opt);
wdenk29e7f5a2004-03-12 00:14:09 +0000343 }
wdenk3861aa52002-09-27 23:19:37 +0000344 }
wdenk3861aa52002-09-27 23:19:37 +0000345
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000346 debug("[BOOTP] Received fields:\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500347 if (net_netmask.s_addr)
348 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000349
Joe Hershberger5874dec2015-04-08 01:41:01 -0500350 if (net_gateway.s_addr)
351 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000352
Joe Hershberger290c8992015-04-08 01:41:02 -0500353 if (net_boot_file_expected_size_in_blocks)
354 debug("net_boot_file_expected_size_in_blocks : %d\n",
355 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000356
Joe Hershberger6d236432015-04-08 01:41:03 -0500357 if (net_hostname[0])
358 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000359
Joe Hershberger6d236432015-04-08 01:41:03 -0500360 if (net_root_path[0])
361 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000362
Joe Hershberger6d236432015-04-08 01:41:03 -0500363 if (net_nis_domain[0])
364 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000365
Luuk Paulussen6380e012011-05-16 18:29:19 +0000366#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Chris Packham3a2ae812018-05-03 20:19:03 +1200367 if (net_ntp_server.s_addr)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500368 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen6380e012011-05-16 18:29:19 +0000369#endif
wdenk3861aa52002-09-27 23:19:37 +0000370}
Simon Glassab068eb2011-06-13 16:13:12 -0700371
wdenk3861aa52002-09-27 23:19:37 +0000372/*
373 * Handle a BOOTP received packet.
374 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500375static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
376 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000377{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500378 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000379
Robin Getz9e0a4d62009-07-23 03:01:03 -0400380 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500381 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000382
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500383 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000384
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000385 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +0200386 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000387 return;
388
389 /*
wdenk29e7f5a2004-03-12 00:14:09 +0000390 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000391 */
Uri Mashiach4892d392017-01-19 10:51:45 +0200392#if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
393 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000394#endif
395
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500396 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000397
398 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500399 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500400 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000401
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500402 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +0000403 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000404
Robin Getz9e0a4d62009-07-23 03:01:03 -0400405 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000406
Simon Glass5234ad12011-10-27 06:24:32 +0000407 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000408}
Jon Loeligera9807e52007-07-10 11:05:02 -0500409#endif
wdenk3861aa52002-09-27 23:19:37 +0000410
411/*
412 * Timeout on BOOTP/DHCP request.
413 */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500414static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000415{
Stephen Warren69e1e352014-07-25 17:30:48 -0600416 ulong time_taken = get_timer(bootp_start);
Sean Edmond0c962d42024-05-08 19:39:02 -0700417 int rand_minus_plus_100;
Stephen Warren69e1e352014-07-25 17:30:48 -0600418
Alexandre Messier15971322016-02-01 17:08:57 -0500419 if (time_taken >= time_taken_max) {
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000420#ifdef CONFIG_BOOTP_MAY_FAIL
Joe Hershberger724cc6a2017-11-07 18:13:40 -0800421 char *ethrotate;
422
423 ethrotate = env_get("ethrotate");
424 if ((ethrotate && strcmp(ethrotate, "no") == 0) ||
425 net_restart_wrap) {
426 puts("\nRetry time exceeded\n");
427 net_set_state(NETLOOP_FAIL);
428 } else
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000429#endif
Joe Hershberger724cc6a2017-11-07 18:13:40 -0800430 {
431 puts("\nRetry time exceeded; starting again\n");
432 net_start_again();
433 }
wdenk3861aa52002-09-27 23:19:37 +0000434 } else {
Stephen Warren69e1e352014-07-25 17:30:48 -0600435 bootp_timeout *= 2;
Sean Edmond0c962d42024-05-08 19:39:02 -0700436 if (bootp_timeout > retransmit_period_max_ms)
437 bootp_timeout = retransmit_period_max_ms;
438
439 /* Randomize by adding bootp_timeout*RAND, where RAND
440 * is a randomization factor between -0.1..+0.1
441 */
442 srand(get_ticks() + rand());
443 rand_minus_plus_100 = ((rand() % 200) - 100);
444 bootp_timeout = bootp_timeout +
445 (((int)bootp_timeout * rand_minus_plus_100) / 1000);
446
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500447 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500448 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000449 }
450}
451
Ilya Yanok30876582012-09-17 10:26:25 +0000452#define put_vci(e, str) \
453 do { \
454 size_t vci_strlen = strlen(str); \
455 *e++ = 60; /* Vendor Class Identifier */ \
456 *e++ = vci_strlen; \
457 memcpy(e, str, vci_strlen); \
458 e += vci_strlen; \
459 } while (0)
460
Alexander Grafea73a042016-05-06 21:01:02 +0200461static u8 *add_vci(u8 *e)
462{
Alexander Graf3661aac2016-05-06 21:01:07 +0200463 char *vci = NULL;
Simon Glass64b723f2017-08-03 12:22:12 -0600464 char *env_vci = env_get("bootp_vci");
Alexander Graf3661aac2016-05-06 21:01:07 +0200465
Alexander Grafea73a042016-05-06 21:01:02 +0200466#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
Alexander Graf3661aac2016-05-06 21:01:07 +0200467 vci = CONFIG_SPL_NET_VCI_STRING;
Alexander Grafea73a042016-05-06 21:01:02 +0200468#elif defined(CONFIG_BOOTP_VCI_STRING)
Alexander Graf3661aac2016-05-06 21:01:07 +0200469 vci = CONFIG_BOOTP_VCI_STRING;
Alexander Grafea73a042016-05-06 21:01:02 +0200470#endif
471
Alexander Graf3661aac2016-05-06 21:01:07 +0200472 if (env_vci)
473 vci = env_vci;
474
475 if (vci)
476 put_vci(e, vci);
477
Alexander Grafea73a042016-05-06 21:01:02 +0200478 return e;
479}
480
wdenk3861aa52002-09-27 23:19:37 +0000481/*
482 * Initialize BOOTP extension fields in the request.
483 */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500484#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500485static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
486 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000487{
wdenk29e7f5a2004-03-12 00:14:09 +0000488 u8 *start = e;
489 u8 *cnt;
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200490#ifdef CONFIG_LIB_UUID
Jason Hobbsea3202812011-08-31 05:37:31 +0000491 char *uuid;
Jason Hobbsea3202812011-08-31 05:37:31 +0000492#endif
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200493 int clientarch = -1;
wdenk29e7f5a2004-03-12 00:14:09 +0000494
Jon Loeliger5336a762007-07-09 22:08:34 -0500495#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000496 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000497#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500498#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200499 char *hostname;
stroesee0aadfb2003-08-28 14:17:32 +0000500#endif
wdenk3861aa52002-09-27 23:19:37 +0000501
wdenk29e7f5a2004-03-12 00:14:09 +0000502 *e++ = 99; /* RFC1048 Magic Cookie */
503 *e++ = 130;
504 *e++ = 83;
505 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000506
wdenk29e7f5a2004-03-12 00:14:09 +0000507 *e++ = 53; /* DHCP Message Type */
508 *e++ = 1;
509 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000510
wdenk29e7f5a2004-03-12 00:14:09 +0000511 *e++ = 57; /* Maximum DHCP Message Size */
512 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000513 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
514 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000515
Joe Hershberger5874dec2015-04-08 01:41:01 -0500516 if (server_ip.s_addr) {
517 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000518
wdenk29e7f5a2004-03-12 00:14:09 +0000519 *e++ = 54; /* ServerID */
520 *e++ = 4;
521 *e++ = tmp >> 24;
522 *e++ = tmp >> 16;
523 *e++ = tmp >> 8;
524 *e++ = tmp & 0xff;
525 }
wdenk3861aa52002-09-27 23:19:37 +0000526
Joe Hershberger5874dec2015-04-08 01:41:01 -0500527 if (requested_ip.s_addr) {
528 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000529
wdenk29e7f5a2004-03-12 00:14:09 +0000530 *e++ = 50; /* Requested IP */
531 *e++ = 4;
532 *e++ = tmp >> 24;
533 *e++ = tmp >> 16;
534 *e++ = tmp >> 8;
535 *e++ = tmp & 0xff;
536 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500537#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Simon Glass64b723f2017-08-03 12:22:12 -0600538 hostname = env_get("hostname");
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000539 if (hostname) {
540 int hostnamelen = strlen(hostname);
wdenk29e7f5a2004-03-12 00:14:09 +0000541
542 *e++ = 12; /* Hostname */
543 *e++ = hostnamelen;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000544 memcpy(e, hostname, hostnamelen);
wdenk29e7f5a2004-03-12 00:14:09 +0000545 e += hostnamelen;
546 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000547#endif
548
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200549#ifdef CONFIG_BOOTP_PXE_CLIENTARCH
Jason Hobbsea3202812011-08-31 05:37:31 +0000550 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200551#endif
552
Simon Glass64b723f2017-08-03 12:22:12 -0600553 if (env_get("bootp_arch"))
Simon Glass22c34c22017-08-03 12:22:13 -0600554 clientarch = env_get_ulong("bootp_arch", 16, clientarch);
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200555
556 if (clientarch > 0) {
557 *e++ = 93; /* Client System Architecture */
558 *e++ = 2;
559 *e++ = (clientarch >> 8) & 0xff;
560 *e++ = clientarch & 0xff;
561 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000562
563 *e++ = 94; /* Client Network Interface Identifier */
564 *e++ = 3;
565 *e++ = 1; /* type field for UNDI */
566 *e++ = 0; /* major revision */
567 *e++ = 0; /* minor revision */
568
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200569#ifdef CONFIG_LIB_UUID
Simon Glass64b723f2017-08-03 12:22:12 -0600570 uuid = env_get("pxeuuid");
Jason Hobbsea3202812011-08-31 05:37:31 +0000571
572 if (uuid) {
573 if (uuid_str_valid(uuid)) {
574 *e++ = 97; /* Client Machine Identifier */
575 *e++ = 17;
576 *e++ = 0; /* type 0 - UUID */
577
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200578 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsea3202812011-08-31 05:37:31 +0000579 e += 16;
580 } else {
581 printf("Invalid pxeuuid: %s\n", uuid);
582 }
583 }
Ilya Yanok30876582012-09-17 10:26:25 +0000584#endif
Jason Hobbsea3202812011-08-31 05:37:31 +0000585
Alexander Grafea73a042016-05-06 21:01:02 +0200586 e = add_vci(e);
stroesee0aadfb2003-08-28 14:17:32 +0000587
Jon Loeliger5336a762007-07-09 22:08:34 -0500588#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000589 x = dhcp_vendorex_prep(e);
590 if (x)
wdenk29e7f5a2004-03-12 00:14:09 +0000591 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000592#endif
593
wdenk29e7f5a2004-03-12 00:14:09 +0000594 *e++ = 55; /* Parameter Request List */
595 cnt = e++; /* Pointer to count of requested items */
596 *cnt = 0;
Jon Loeliger5336a762007-07-09 22:08:34 -0500597#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000598 *e++ = 1; /* Subnet Mask */
599 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000600#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500601#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000602 *e++ = 2;
603 *cnt += 1;
604#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500605#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000606 *e++ = 3; /* Router Option */
607 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000608#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500609#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000610 *e++ = 6; /* DNS Server(s) */
611 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000612#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500613#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000614 *e++ = 12; /* Hostname */
615 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000616#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500617#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000618 *e++ = 13; /* Boot File Size */
619 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000620#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500621#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000622 *e++ = 17; /* Boot path */
623 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000624#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500625#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000626 *e++ = 40; /* NIS Domain name request */
627 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000628#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500629#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000630 *e++ = 42;
631 *cnt += 1;
632#endif
Sean Edmond57867112023-07-25 16:20:30 -0700633 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION)) {
Sean Edmond0760efe2024-05-08 19:39:01 -0700634 *e++ = DHCP_OPTION_PXE_CONFIG_FILE; /* PXELINUX Config File */
Sean Edmond57867112023-07-25 16:20:30 -0700635 *cnt += 1;
636 }
Jason Liu1c7dca52010-11-14 12:23:09 +0800637 /* no options, so back up to avoid sending an empty request list */
638 if (*cnt == 0)
639 e -= 2;
640
wdenk29e7f5a2004-03-12 00:14:09 +0000641 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000642
wdenk29e7f5a2004-03-12 00:14:09 +0000643 /* Pad to minimal length */
Tom Rini364d0022023-01-10 11:19:45 -0500644#ifdef CFG_DHCP_MIN_EXT_LEN
645 while ((e - start) < CFG_DHCP_MIN_EXT_LEN)
wdenk29e7f5a2004-03-12 00:14:09 +0000646 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000647#endif
648
wdenk29e7f5a2004-03-12 00:14:09 +0000649 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000650}
651
Jon Loeligera9807e52007-07-10 11:05:02 -0500652#else
wdenk3861aa52002-09-27 23:19:37 +0000653/*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000654 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000655 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500656static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000657{
wdenk29e7f5a2004-03-12 00:14:09 +0000658 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000659
wdenk29e7f5a2004-03-12 00:14:09 +0000660 *e++ = 99; /* RFC1048 Magic Cookie */
661 *e++ = 130;
662 *e++ = 83;
663 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000664
Jon Loeliger54f35c22007-07-09 17:45:14 -0500665#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000666 *e++ = 53; /* DHCP Message Type */
667 *e++ = 1;
668 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000669
wdenk29e7f5a2004-03-12 00:14:09 +0000670 *e++ = 57; /* Maximum DHCP Message Size */
671 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000672 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
673 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeligera9807e52007-07-10 11:05:02 -0500674#endif
wdenk3861aa52002-09-27 23:19:37 +0000675
Walter Stoll8182a3a2021-10-12 11:01:59 +0000676 e = add_vci(e);
Ilya Yanok30876582012-09-17 10:26:25 +0000677
Jon Loeliger5336a762007-07-09 22:08:34 -0500678#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000679 *e++ = 1; /* Subnet mask request */
680 *e++ = 4;
681 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000682#endif
683
Jon Loeliger5336a762007-07-09 22:08:34 -0500684#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000685 *e++ = 3; /* Default gateway request */
686 *e++ = 4;
687 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000688#endif
689
Jon Loeliger5336a762007-07-09 22:08:34 -0500690#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000691 *e++ = 6; /* Domain Name Server */
692 *e++ = 4;
693 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000694#endif
695
Jon Loeliger5336a762007-07-09 22:08:34 -0500696#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000697 *e++ = 12; /* Host name request */
698 *e++ = 32;
699 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000700#endif
701
Jon Loeliger5336a762007-07-09 22:08:34 -0500702#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000703 *e++ = 13; /* Boot file size */
704 *e++ = 2;
705 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000706#endif
707
Jon Loeliger5336a762007-07-09 22:08:34 -0500708#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000709 *e++ = 17; /* Boot path */
710 *e++ = 32;
711 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000712#endif
713
Jon Loeliger5336a762007-07-09 22:08:34 -0500714#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000715 *e++ = 40; /* NIS Domain name request */
716 *e++ = 32;
717 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000718#endif
Luuk Paulussen6380e012011-05-16 18:29:19 +0000719#if defined(CONFIG_BOOTP_NTPSERVER)
720 *e++ = 42;
721 *e++ = 4;
722 e += 4;
723#endif
wdenk3861aa52002-09-27 23:19:37 +0000724
wdenk29e7f5a2004-03-12 00:14:09 +0000725 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000726
Andre Renaud23298cd2016-05-05 07:28:08 -0600727 /*
728 * If nothing in list, remove it altogether. Some DHCP servers get
729 * upset by this minor faux pas and do not respond at all.
730 */
731 if (e == start + 3) {
732 printf("*** Warning: no DHCP options requested\n");
733 e -= 3;
734 }
735
wdenk29e7f5a2004-03-12 00:14:09 +0000736 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000737}
Jon Loeligera9807e52007-07-10 11:05:02 -0500738#endif
wdenk3861aa52002-09-27 23:19:37 +0000739
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500740void bootp_reset(void)
Stephen Warren69e1e352014-07-25 17:30:48 -0600741{
Thierry Reding8977cda2014-08-19 10:21:24 +0200742 bootp_num_ids = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500743 bootp_try = 0;
Stephen Warren69e1e352014-07-25 17:30:48 -0600744 bootp_start = get_timer(0);
Sean Edmond0c962d42024-05-08 19:39:02 -0700745
746 bootp_timeout = env_get_ulong("bootpretransmitperiodinit", 10, RETRANSMIT_PERIOD_INIT_MS);
Stephen Warren69e1e352014-07-25 17:30:48 -0600747}
748
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500749void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000750{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000751 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500752 struct bootp_hdr *bp;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000753 int extlen, pktlen, iplen;
754 int eth_hdr_size;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000755#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek764c29d2014-07-11 11:39:37 +0200756 ulong rand_ms;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000757#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500758 struct in_addr zero_ip;
759 struct in_addr bcast_ip;
Alexandre Messier15971322016-02-01 17:08:57 -0500760 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000761
Simon Glass768cbf02011-12-10 11:08:06 +0000762 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger54f35c22007-07-09 17:45:14 -0500763#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000764 dhcp_state = INIT;
765#endif
766
Simon Glass64b723f2017-08-03 12:22:12 -0600767 ep = env_get("bootpretryperiod");
Alexandre Messier15971322016-02-01 17:08:57 -0500768 if (ep != NULL)
Simon Glassff9b9032021-07-24 09:03:30 -0600769 time_taken_max = dectoul(ep, NULL);
Alexandre Messier15971322016-02-01 17:08:57 -0500770 else
771 time_taken_max = TIMEOUT_MS;
772
Sean Edmond0c962d42024-05-08 19:39:02 -0700773 retransmit_period_max_ms = env_get_ulong("bootpretransmitperiodmax", 10,
774 RETRANSMIT_PERIOD_MAX_MS);
775
wdenk3861aa52002-09-27 23:19:37 +0000776#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500777 if (bootp_try == 0)
Joe Hershberger797f2c52012-05-23 07:57:58 +0000778 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000779
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500780 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
781 rand_ms = rand() >> (22 - bootp_try);
Joe Hershberger797f2c52012-05-23 07:57:58 +0000782 else /* After 3rd BOOTP request max 8192 * 1ms */
783 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000784
Joe Hershberger797f2c52012-05-23 07:57:58 +0000785 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek764c29d2014-07-11 11:39:37 +0200786 mdelay(rand_ms);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000787
wdenk3861aa52002-09-27 23:19:37 +0000788#endif /* CONFIG_BOOTP_RANDOM_DELAY */
789
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500790 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500791 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000792 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000793
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500794 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000795 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000796
797 /*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000798 * Next line results in incorrect packet size being transmitted,
799 * resulting in errors in some DHCP servers, reporting missing bytes.
800 * Size must be set in packet header after extension length has been
801 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000802 * C. Hallinan, DS4.COM, Inc.
803 */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000804 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500805 sizeof (struct bootp_hdr)); */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000806 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000807 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000808
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500809 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000810 bp->bp_op = OP_BOOTREQUEST;
811 bp->bp_htype = HWT_ETHER;
812 bp->bp_hlen = HWL_ETHER;
813 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +0200814 /*
815 * according to RFC1542, should be 0 on first request, secs since
816 * first request otherwise
817 */
818 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500819 zero_ip.s_addr = 0;
820 net_write_ip(&bp->bp_ciaddr, zero_ip);
821 net_write_ip(&bp->bp_yiaddr, zero_ip);
822 net_write_ip(&bp->bp_siaddr, zero_ip);
823 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500824 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger290c8992015-04-08 01:41:02 -0500825 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000826
827 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500828#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500829 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
830 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000831#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500832 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeligera9807e52007-07-10 11:05:02 -0500833#endif
wdenk3861aa52002-09-27 23:19:37 +0000834
Sean Edmond0c962d42024-05-08 19:39:02 -0700835 /* Only generate a new transaction ID for each new BOOTP request */
836 if (bootp_try == 1) {
837 /*
838 * Bootp ID is the lower 4 bytes of our ethernet address
839 * plus the current time in ms.
840 */
841 bootp_id = ((u32)net_ethaddr[2] << 24)
842 | ((u32)net_ethaddr[3] << 16)
843 | ((u32)net_ethaddr[4] << 8)
844 | (u32)net_ethaddr[5];
845 bootp_id += get_timer(0);
846 bootp_id = htonl(bootp_id);
847 bootp_add_id(bootp_id);
848 net_copy_u32(&bp->bp_id, &bootp_id);
849 } else {
850 net_copy_u32(&bp->bp_id, &bootp_id);
851 }
wdenk3861aa52002-09-27 23:19:37 +0000852
853 /*
854 * Calculate proper packet lengths taking into account the
855 * variable size of the options field
856 */
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000857 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
858 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500859 bcast_ip.s_addr = 0xFFFFFFFFL;
860 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500861 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000862
Jon Loeliger54f35c22007-07-09 17:45:14 -0500863#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000864 dhcp_state = SELECTING;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500865 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000866#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500867 net_set_udp_handler(bootp_handler);
Jon Loeligera9807e52007-07-10 11:05:02 -0500868#endif
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500869 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000870}
871
Jon Loeliger54f35c22007-07-09 17:45:14 -0500872#if defined(CONFIG_CMD_DHCP)
Stefan Brüns79f724c2015-09-04 00:31:49 +0200873static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000874{
wdenk3861aa52002-09-27 23:19:37 +0000875 int oplen, size;
Wolfgang Denk2b184222009-09-11 09:05:32 +0200876#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
877 int *to_ptr;
878#endif
wdenk3861aa52002-09-27 23:19:37 +0000879
wdenk29e7f5a2004-03-12 00:14:09 +0000880 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000881 oplen = *(popt + 1);
wdenk29e7f5a2004-03-12 00:14:09 +0000882 switch (*popt) {
Stefan Brüns44318292015-08-28 10:15:54 +0200883 case 0:
884 oplen = -1; /* Pad omits len byte */
885 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000886 case 1:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500887 net_copy_ip(&net_netmask, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000888 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500889#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000890 case 2: /* Time offset */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500891 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500892 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500893 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkb4ad9622005-04-01 00:25:43 +0000894 break;
895#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000896 case 3:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500897 net_copy_ip(&net_gateway, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000898 break;
899 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500900 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500901#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000902 if (*(popt + 1) > 4)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500903 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000904#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000905 break;
906 case 12:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000907 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500908 sizeof(net_hostname), oplen);
909 memcpy(&net_hostname, popt + 2, size);
910 net_hostname[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000911 break;
912 case 15: /* Ignore Domain Name Option */
913 break;
914 case 17:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000915 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500916 sizeof(net_root_path), oplen);
917 memcpy(&net_root_path, popt + 2, size);
918 net_root_path[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000919 break;
Brian Rzycki5a672a22012-09-11 09:22:53 +0000920 case 28: /* Ignore Broadcast Address Option */
921 break;
Charles Hardinac178f72024-04-12 13:45:33 -0700922 case 40: /* NIS Domain name */
923 if (net_nis_domain[0] == 0) {
924 size = truncate_sz("NIS Domain Name",
925 sizeof(net_nis_domain), size);
926 memcpy(&net_nis_domain, popt + 2, size);
927 net_nis_domain[size] = 0;
928 }
929 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500930#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000931 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500932 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkb4ad9622005-04-01 00:25:43 +0000933 break;
934#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000935 case 51:
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500936 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000937 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200938 case 52:
939 dhcp_option_overload = popt[2];
940 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000941 case 53: /* Ignore Message Type Option */
942 break;
943 case 54:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500944 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000945 break;
946 case 58: /* Ignore Renewal Time Option */
947 break;
948 case 59: /* Ignore Rebinding Time Option */
949 break;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100950 case 66: /* Ignore TFTP server name */
951 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200952 case 67: /* Bootfile option */
Alexander Graff43bf5d2018-06-15 10:29:27 +0200953 if (!net_boot_file_name_explicit) {
954 size = truncate_sz("Bootfile",
955 sizeof(net_boot_file_name),
956 oplen);
957 memcpy(&net_boot_file_name, popt + 2, size);
958 net_boot_file_name[size] = 0;
959 }
Wolfgang Denk012429c2006-03-12 18:26:46 +0100960 break;
Sean Edmond0760efe2024-05-08 19:39:01 -0700961 case DHCP_OPTION_PXE_CONFIG_FILE: /* PXELINUX Config File */
Sean Edmond57867112023-07-25 16:20:30 -0700962 if (IS_ENABLED(CONFIG_BOOTP_PXE_DHCP_OPTION)) {
963 /* In case it has already been allocated when get DHCP Offer packet,
964 * free first to avoid memory leak.
965 */
966 if (pxelinux_configfile)
967 free(pxelinux_configfile);
968
969 pxelinux_configfile = (char *)malloc((oplen + 1) * sizeof(char));
970
971 if (pxelinux_configfile)
972 strlcpy(pxelinux_configfile, popt + 2, oplen + 1);
973 else
974 printf("Error: Failed to allocate pxelinux_configfile\n");
975 }
976 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000977 default:
Jon Loeliger5336a762007-07-09 22:08:34 -0500978#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000979 if (dhcp_vendorex_proc(popt))
wdenk57b2d802003-06-27 21:31:46 +0000980 break;
wdenk3861aa52002-09-27 23:19:37 +0000981#endif
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000982 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500983 " %d\n", *popt);
wdenk29e7f5a2004-03-12 00:14:09 +0000984 break;
wdenk3861aa52002-09-27 23:19:37 +0000985 }
986 popt += oplen + 2; /* Process next option */
987 }
988}
989
Stefan Brüns79f724c2015-09-04 00:31:49 +0200990static void dhcp_packet_process_options(struct bootp_hdr *bp)
991{
992 uchar *popt = (uchar *)&bp->bp_vend[4];
993 uchar *end = popt + BOOTP_HDR_SIZE;
994
995 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
996 return;
997
998 dhcp_option_overload = 0;
999
1000 /*
1001 * The 'options' field MUST be interpreted first, 'file' next,
1002 * 'sname' last.
1003 */
1004 dhcp_process_options(popt, end);
1005
1006 if (dhcp_option_overload & OVERLOAD_FILE) {
1007 popt = (uchar *)bp->bp_file;
1008 end = popt + sizeof(bp->bp_file);
1009 dhcp_process_options(popt, end);
1010 }
1011
1012 if (dhcp_option_overload & OVERLOAD_SNAME) {
1013 popt = (uchar *)bp->bp_sname;
1014 end = popt + sizeof(bp->bp_sname);
1015 dhcp_process_options(popt, end);
1016 }
1017}
1018
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001019static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +00001020{
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -05001021 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +00001022 return -1;
1023
1024 popt += 4;
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001025 while (*popt != 0xff) {
1026 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +00001027 return *(popt + 2);
Stefan Brüns44318292015-08-28 10:15:54 +02001028 if (*popt == 0) {
1029 /* Pad */
1030 popt += 1;
1031 } else {
1032 /* Scan through all options */
1033 popt += *(popt + 1) + 2;
1034 }
wdenk3861aa52002-09-27 23:19:37 +00001035 }
1036 return -1;
1037}
1038
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001039static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +00001040{
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001041 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001042 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +00001043 int pktlen, iplen, extlen;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001044 int eth_hdr_size;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001045 struct in_addr offered_ip;
1046 struct in_addr zero_ip;
1047 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +00001048
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001049 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001050 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001051 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +00001052
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001053 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001054 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +00001055
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001056 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001057 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +00001058
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001059 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001060 bp->bp_op = OP_BOOTREQUEST;
1061 bp->bp_htype = HWT_ETHER;
1062 bp->bp_hlen = HWL_ETHER;
1063 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +02001064 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001065 /* Do not set the client IP, your IP, or server IP yet, since it
1066 * hasn't been ACK'ed by the server yet */
Justin Flammia01f256b2007-10-29 17:40:35 -04001067
Wolfgang Denk8a933fb2006-10-12 00:01:08 +02001068 /*
Wolfgang Denk68e83a82006-10-09 01:26:14 +02001069 * RFC3046 requires Relay Agents to discard packets with
1070 * nonzero and offered giaddr
1071 */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001072 zero_ip.s_addr = 0;
1073 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denk68e83a82006-10-09 01:26:14 +02001074
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001075 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierad28a312016-01-28 11:19:02 -05001076 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +00001077
1078 /*
1079 * ID is the id of the OFFER packet
1080 */
1081
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -05001082 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +00001083
1084 /*
1085 * Copy options from OFFER packet if present
1086 */
Justin Flammia01f256b2007-10-29 17:40:35 -04001087
1088 /* Copy offered IP into the parameters request list */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001089 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
1090 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
1091 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +00001092
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001093 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
1094 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001095 bcast_ip.s_addr = 0xFFFFFFFFL;
1096 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +00001097
Joe Hershbergerb1e94762012-05-23 07:59:11 +00001098 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001099 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +00001100}
1101
1102/*
1103 * Handle DHCP received packets.
1104 */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001105static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1106 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +00001107{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001108 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001109
Robin Getz9e0a4d62009-07-23 03:01:03 -04001110 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001111 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001112
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001113 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +02001114 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001115 return;
1116
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001117 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1118 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001119
Lyle Franklin73fcbc72019-08-05 06:23:42 -04001120 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0) {
1121#if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1122 store_bootp_params(bp);
1123#endif
Peng Fan67e72e92016-01-07 15:28:23 +08001124 return;
Lyle Franklin73fcbc72019-08-05 06:23:42 -04001125 }
Peng Fan67e72e92016-01-07 15:28:23 +08001126
wdenk3861aa52002-09-27 23:19:37 +00001127 switch (dhcp_state) {
1128 case SELECTING:
1129 /*
1130 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001131 * to arrive. Then select one, and generate DHCPREQUEST
1132 * response. If filename is in format we recognize, assume it
1133 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001134 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001135 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001136#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001137 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001138 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001139 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001140#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Sean Anderson123d9002023-10-14 16:47:53 -04001141 if (CONFIG_IS_ENABLED(UNIT_TEST) &&
1142 dhcp_message_type((u8 *)bp->bp_vend) == -1) {
1143 debug("got BOOTP response; transitioning to BOUND\n");
1144 goto dhcp_got_bootp;
1145 }
Stefan Brüns79f724c2015-09-04 00:31:49 +02001146 dhcp_packet_process_options(bp);
Jan Kiszkaf1389822022-10-14 18:10:06 +02001147 if (CONFIG_IS_ENABLED(EFI_LOADER) &&
Simon Glass8ce7c822023-02-05 15:40:20 -07001148 IS_ENABLED(CONFIG_NETDEVICES))
Jan Kiszkaf1389822022-10-14 18:10:06 +02001149 efi_net_set_dhcp_ack(pkt, len);
wdenk3861aa52002-09-27 23:19:37 +00001150
Lyle Franklin73fcbc72019-08-05 06:23:42 -04001151#if defined(CONFIG_SERVERIP_FROM_PROXYDHCP)
1152 if (!net_server_ip.s_addr)
1153 udelay(CONFIG_SERVERIP_FROM_PROXYDHCP_DELAY_MS *
1154 1000);
1155#endif /* CONFIG_SERVERIP_FROM_PROXYDHCP */
1156
Robin Getz9e0a4d62009-07-23 03:01:03 -04001157 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001158 dhcp_state = REQUESTING;
stroese5fa6e902003-04-10 13:26:44 +00001159
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001160 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001161 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001162#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001163 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001164#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001165
1166 return;
1167 break;
1168 case REQUESTING:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001169 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001170
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001171 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Sean Anderson123d9002023-10-14 16:47:53 -04001172dhcp_got_bootp:
Stefan Brüns79f724c2015-09-04 00:31:49 +02001173 dhcp_packet_process_options(bp);
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001174 /* Store net params from reply */
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001175 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001176 dhcp_state = BOUND;
Thierry Reding8977cda2014-08-19 10:21:24 +02001177 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001178 &net_ip, get_timer(bootp_start));
Stefan Brüns4c60e4a2015-08-30 17:47:17 +02001179 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +00001180 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001181 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001182
Simon Glass5234ad12011-10-27 06:24:32 +00001183 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001184 return;
1185 }
1186 break;
Remy Bohmer03a44492008-08-20 11:30:28 +02001187 case BOUND:
1188 /* DHCP client bound to address */
1189 break;
wdenk3861aa52002-09-27 23:19:37 +00001190 default:
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001191 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001192 break;
1193 }
wdenk3861aa52002-09-27 23:19:37 +00001194}
1195
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001196void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001197{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001198 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001199}
Wolfgang Denkf7a7f082007-11-03 23:09:27 +01001200#endif /* CONFIG_CMD_DHCP */