blob: 73370a13fe7de813f9bb6ec7ff9daad7982b9047 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk29e7f5a2004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
12#include <command.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020013#include <efi_loader.h>
wdenk3861aa52002-09-27 23:19:37 +000014#include <net.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020015#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000016#include "bootp.h"
wdenk29e7f5a2004-03-12 00:14:09 +000017#include "nfs.h"
Uri Mashiach4892d392017-01-19 10:51:45 +020018#ifdef CONFIG_LED_STATUS
wdenk3861aa52002-09-27 23:19:37 +000019#include <status_led.h>
20#endif
Kim Phillips576175b2012-07-05 13:19:32 +000021#ifdef CONFIG_BOOTP_RANDOM_DELAY
22#include "net_rand.h"
23#endif
wdenk3861aa52002-09-27 23:19:37 +000024
Joe Hershberger8f4b1352012-05-15 08:59:06 +000025#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000026
Stephen Warren69e1e352014-07-25 17:30:48 -060027/*
28 * The timeout for the initial BOOTP/DHCP request used to be described by a
29 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
30 * that counter
31 *
32 * Now that the timeout periods are variable (exponential backoff and retry)
33 * we convert the timeout count to the absolute time it would have take to
34 * execute that many retries, and keep sending retry packets until that time
35 * is reached.
36 */
wdenk29e7f5a2004-03-12 00:14:09 +000037#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger8f4b1352012-05-15 08:59:06 +000038# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000039#else
wdenk29e7f5a2004-03-12 00:14:09 +000040# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000041#endif
Stephen Warren69e1e352014-07-25 17:30:48 -060042#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000043
Joe Hershberger8f4b1352012-05-15 08:59:06 +000044#define PORT_BOOTPS 67 /* BOOTP server UDP port */
45#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000046
Joe Hershberger8f4b1352012-05-15 08:59:06 +000047#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk29e7f5a2004-03-12 00:14:09 +000048#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000049#endif
50
Thierry Reding8977cda2014-08-19 10:21:24 +020051#ifndef CONFIG_BOOTP_ID_CACHE_SIZE
52#define CONFIG_BOOTP_ID_CACHE_SIZE 4
53#endif
54
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -050055u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
Thierry Reding8977cda2014-08-19 10:21:24 +020056unsigned int bootp_num_ids;
Joe Hershberger2fe81a52015-04-08 01:41:09 -050057int bootp_try;
Stephen Warren69e1e352014-07-25 17:30:48 -060058ulong bootp_start;
59ulong bootp_timeout;
Joe Hershberger6d236432015-04-08 01:41:03 -050060char net_nis_domain[32] = {0,}; /* Our NIS domain */
61char net_hostname[32] = {0,}; /* Our hostname */
62char net_root_path[64] = {0,}; /* Our bootpath */
wdenk3861aa52002-09-27 23:19:37 +000063
Alexandre Messier15971322016-02-01 17:08:57 -050064static ulong time_taken_max;
65
Jon Loeliger54f35c22007-07-09 17:45:14 -050066#if defined(CONFIG_CMD_DHCP)
Kim Phillips40c2c032012-10-29 13:34:33 +000067static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -050068static u32 dhcp_leasetime;
Joe Hershberger5874dec2015-04-08 01:41:01 -050069static struct in_addr dhcp_server_ip;
Stefan Brüns345cb832015-09-04 00:31:48 +020070static u8 dhcp_option_overload;
71#define OVERLOAD_FILE 1
72#define OVERLOAD_SNAME 2
Joe Hershberger5874dec2015-04-08 01:41:01 -050073static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
74 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000075
76/* For Debug */
wdenkb02744a2003-04-05 00:53:31 +000077#if 0
78static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000079{
80 switch (type) {
wdenk29e7f5a2004-03-12 00:14:09 +000081 case 1: return "DHCPDISCOVER"; break;
82 case 2: return "DHCPOFFER"; break;
83 case 3: return "DHCPREQUEST"; break;
84 case 4: return "DHCPDECLINE"; break;
85 case 5: return "DHCPACK"; break;
86 case 6: return "DHCPNACK"; break;
87 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000088 default: return "UNKNOWN/INVALID MSG TYPE"; break;
89 }
90}
wdenkb02744a2003-04-05 00:53:31 +000091#endif
Jon Loeligera9807e52007-07-10 11:05:02 -050092#endif
wdenk3861aa52002-09-27 23:19:37 +000093
Thierry Reding8977cda2014-08-19 10:21:24 +020094static void bootp_add_id(ulong id)
95{
96 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
97 size_t size = sizeof(bootp_ids) - sizeof(id);
98
99 memmove(bootp_ids, &bootp_ids[1], size);
100 bootp_ids[bootp_num_ids - 1] = id;
101 } else {
102 bootp_ids[bootp_num_ids] = id;
103 bootp_num_ids++;
104 }
105}
106
107static bool bootp_match_id(ulong id)
108{
109 unsigned int i;
110
111 for (i = 0; i < bootp_num_ids; i++)
112 if (bootp_ids[i] == id)
113 return true;
114
115 return false;
116}
117
Stefan Brünsa48cd932015-08-27 23:53:26 +0200118static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
119 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000120{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500121 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000122 int retval = 0;
123
124 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
125 retval = -1;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500126 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000127 retval = -2;
Stefan Brünsa48cd932015-08-27 23:53:26 +0200128 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000129 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000130 else if (bp->bp_htype != HWT_ETHER)
131 retval = -4;
132 else if (bp->bp_hlen != HWL_ETHER)
133 retval = -5;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500134 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000135 retval = -6;
Anton Perssonbf27a4c2016-03-17 09:38:21 +0100136 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
137 retval = -7;
wdenk3861aa52002-09-27 23:19:37 +0000138
Robin Getz9e0a4d62009-07-23 03:01:03 -0400139 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000140
141 return retval;
142}
143
144/*
145 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
146 */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500147static void store_net_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000148{
Wilson Callan22bcd6e2007-07-28 10:56:13 -0400149#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500150 struct in_addr tmp_ip;
Joe Hershberger9d37a582012-05-23 07:59:18 +0000151
Joe Hershberger5874dec2015-04-08 01:41:01 -0500152 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
153 if (tmp_ip.s_addr != 0)
154 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500155 memcpy(net_server_ethaddr,
156 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
Stefan Brüns345cb832015-09-04 00:31:48 +0200157 if (
158#if defined(CONFIG_CMD_DHCP)
159 !(dhcp_option_overload & OVERLOAD_FILE) &&
160#endif
161 (strlen(bp->bp_file) > 0)) {
Joe Hershberger290c8992015-04-08 01:41:02 -0500162 copy_filename(net_boot_file_name, bp->bp_file,
163 sizeof(net_boot_file_name));
Stefan Brüns345cb832015-09-04 00:31:48 +0200164 }
wdenk3861aa52002-09-27 23:19:37 +0000165
Joe Hershberger290c8992015-04-08 01:41:02 -0500166 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000167
168 /* Propagate to environment:
wdenk57b2d802003-06-27 21:31:46 +0000169 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000170 * not contain a new value
171 */
Joe Hershberger290c8992015-04-08 01:41:02 -0500172 if (*net_boot_file_name)
Simon Glass6a38e412017-08-03 12:22:09 -0600173 env_set("bootfile", net_boot_file_name);
Wu, Josha9c2cbd2014-11-18 13:07:08 +0800174#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500175 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000176}
177
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000178static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000179{
180 if (curlen >= maxlen) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000181 printf("*** WARNING: %s is too long (%d - max: %d)"
182 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000183 curlen = maxlen - 1;
184 }
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000185 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000186}
187
Jon Loeliger54f35c22007-07-09 17:45:14 -0500188#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000189
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500190static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000191{
wdenk29e7f5a2004-03-12 00:14:09 +0000192 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000193
Robin Getz9e0a4d62009-07-23 03:01:03 -0400194 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500195 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000196
Joe Hershberger290c8992015-04-08 01:41:02 -0500197 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000198
wdenk29e7f5a2004-03-12 00:14:09 +0000199 switch (*ext) {
200 /* Fixed length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000201 case 1: /* Subnet mask */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500202 if (net_netmask.s_addr == 0)
203 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000204 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000205 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000206 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000207 /* Variable length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000208 case 3: /* Gateways list */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500209 if (net_gateway.s_addr == 0)
210 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000211 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000212 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000213 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000214 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000215 break;
216 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500217 if (net_dns_server.s_addr == 0)
218 net_copy_ip(&net_dns_server,
219 (struct in_addr *)(ext + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500220#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500221 if ((net_dns_server2.s_addr == 0) && (size > 4))
222 net_copy_ip(&net_dns_server2,
223 (struct in_addr *)(ext + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000224#endif
wdenk3861aa52002-09-27 23:19:37 +0000225 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000226 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000227 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000228 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000230 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000231 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000232 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000233 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000234 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000235 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000236 case 12: /* Host name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500237 if (net_hostname[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000238 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500239 sizeof(net_hostname), size);
240 memcpy(&net_hostname, ext + 2, size);
241 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000242 }
243 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000244 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000245 if (size == 2)
Joe Hershberger290c8992015-04-08 01:41:02 -0500246 net_boot_file_expected_size_in_blocks =
247 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000248 else if (size == 4)
Joe Hershberger290c8992015-04-08 01:41:02 -0500249 net_boot_file_expected_size_in_blocks =
250 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000251 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000252 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000253 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000254 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000255 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000256 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000257 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000258 case 17: /* Root path */
Joe Hershberger6d236432015-04-08 01:41:03 -0500259 if (net_root_path[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000260 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500261 sizeof(net_root_path), size);
262 memcpy(&net_root_path, ext + 2, size);
263 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000264 }
265 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000266 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000267 /*
wdenk57b2d802003-06-27 21:31:46 +0000268 * This can be used to send the information of the
269 * vendor area in another file that the client can
270 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000271 */
272 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000273 /* IP host layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000274 case 40: /* NIS Domain name */
Joe Hershberger6d236432015-04-08 01:41:03 -0500275 if (net_nis_domain[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000276 size = truncate_sz("NIS Domain Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500277 sizeof(net_nis_domain), size);
278 memcpy(&net_nis_domain, ext + 2, size);
279 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000280 }
281 break;
Luuk Paulussen6380e012011-05-16 18:29:19 +0000282#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
283 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500284 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen6380e012011-05-16 18:29:19 +0000285 break;
286#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000287 /* Application layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000288 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000289 /*
wdenk57b2d802003-06-27 21:31:46 +0000290 * Binary information to exchange specific
291 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000292 */
293 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000294 /* Reserved (custom) fields (128..254) */
295 }
wdenk3861aa52002-09-27 23:19:37 +0000296}
297
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500298static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000299{
wdenk29e7f5a2004-03-12 00:14:09 +0000300 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000301
Robin Getz9e0a4d62009-07-23 03:01:03 -0400302 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000303
wdenk29e7f5a2004-03-12 00:14:09 +0000304 while ((ext < end) && (*ext != 0xff)) {
305 if (*ext == 0) {
306 ext++;
307 } else {
308 u8 *opt = ext;
309
310 ext += ext[1] + 2;
311 if (ext <= end)
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500312 bootp_process_vendor_field(opt);
wdenk29e7f5a2004-03-12 00:14:09 +0000313 }
wdenk3861aa52002-09-27 23:19:37 +0000314 }
wdenk3861aa52002-09-27 23:19:37 +0000315
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000316 debug("[BOOTP] Received fields:\n");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500317 if (net_netmask.s_addr)
318 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000319
Joe Hershberger5874dec2015-04-08 01:41:01 -0500320 if (net_gateway.s_addr)
321 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000322
Joe Hershberger290c8992015-04-08 01:41:02 -0500323 if (net_boot_file_expected_size_in_blocks)
324 debug("net_boot_file_expected_size_in_blocks : %d\n",
325 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000326
Joe Hershberger6d236432015-04-08 01:41:03 -0500327 if (net_hostname[0])
328 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000329
Joe Hershberger6d236432015-04-08 01:41:03 -0500330 if (net_root_path[0])
331 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000332
Joe Hershberger6d236432015-04-08 01:41:03 -0500333 if (net_nis_domain[0])
334 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000335
Luuk Paulussen6380e012011-05-16 18:29:19 +0000336#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500337 if (net_ntp_server)
338 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen6380e012011-05-16 18:29:19 +0000339#endif
wdenk3861aa52002-09-27 23:19:37 +0000340}
Simon Glassab068eb2011-06-13 16:13:12 -0700341
wdenk3861aa52002-09-27 23:19:37 +0000342/*
343 * Handle a BOOTP received packet.
344 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500345static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
346 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000347{
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500348 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000349
Robin Getz9e0a4d62009-07-23 03:01:03 -0400350 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500351 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000352
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500353 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000354
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000355 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +0200356 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000357 return;
358
359 /*
wdenk29e7f5a2004-03-12 00:14:09 +0000360 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000361 */
Uri Mashiach4892d392017-01-19 10:51:45 +0200362#if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE)
363 status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000364#endif
365
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500366 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000367
368 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500369 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500370 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000371
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500372 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +0000373 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000374
Robin Getz9e0a4d62009-07-23 03:01:03 -0400375 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000376
Simon Glass5234ad12011-10-27 06:24:32 +0000377 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000378}
Jon Loeligera9807e52007-07-10 11:05:02 -0500379#endif
wdenk3861aa52002-09-27 23:19:37 +0000380
381/*
382 * Timeout on BOOTP/DHCP request.
383 */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500384static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000385{
Stephen Warren69e1e352014-07-25 17:30:48 -0600386 ulong time_taken = get_timer(bootp_start);
387
Alexandre Messier15971322016-02-01 17:08:57 -0500388 if (time_taken >= time_taken_max) {
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000389#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warren69e1e352014-07-25 17:30:48 -0600390 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advans7232ee52012-07-19 01:23:21 +0000391 net_set_state(NETLOOP_FAIL);
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000392#else
Stephen Warren69e1e352014-07-25 17:30:48 -0600393 puts("\nRetry time exceeded; starting again\n");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500394 net_start_again();
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000395#endif
wdenk3861aa52002-09-27 23:19:37 +0000396 } else {
Stephen Warren69e1e352014-07-25 17:30:48 -0600397 bootp_timeout *= 2;
Thierry Reding8977cda2014-08-19 10:21:24 +0200398 if (bootp_timeout > 2000)
399 bootp_timeout = 2000;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500400 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500401 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000402 }
403}
404
Ilya Yanok30876582012-09-17 10:26:25 +0000405#define put_vci(e, str) \
406 do { \
407 size_t vci_strlen = strlen(str); \
408 *e++ = 60; /* Vendor Class Identifier */ \
409 *e++ = vci_strlen; \
410 memcpy(e, str, vci_strlen); \
411 e += vci_strlen; \
412 } while (0)
413
Alexander Grafea73a042016-05-06 21:01:02 +0200414static u8 *add_vci(u8 *e)
415{
Alexander Graf3661aac2016-05-06 21:01:07 +0200416 char *vci = NULL;
Simon Glass64b723f2017-08-03 12:22:12 -0600417 char *env_vci = env_get("bootp_vci");
Alexander Graf3661aac2016-05-06 21:01:07 +0200418
Alexander Grafea73a042016-05-06 21:01:02 +0200419#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
Alexander Graf3661aac2016-05-06 21:01:07 +0200420 vci = CONFIG_SPL_NET_VCI_STRING;
Alexander Grafea73a042016-05-06 21:01:02 +0200421#elif defined(CONFIG_BOOTP_VCI_STRING)
Alexander Graf3661aac2016-05-06 21:01:07 +0200422 vci = CONFIG_BOOTP_VCI_STRING;
Alexander Grafea73a042016-05-06 21:01:02 +0200423#endif
424
Alexander Graf3661aac2016-05-06 21:01:07 +0200425 if (env_vci)
426 vci = env_vci;
427
428 if (vci)
429 put_vci(e, vci);
430
Alexander Grafea73a042016-05-06 21:01:02 +0200431 return e;
432}
433
wdenk3861aa52002-09-27 23:19:37 +0000434/*
435 * Initialize BOOTP extension fields in the request.
436 */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500437#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500438static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
439 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000440{
wdenk29e7f5a2004-03-12 00:14:09 +0000441 u8 *start = e;
442 u8 *cnt;
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200443#ifdef CONFIG_LIB_UUID
Jason Hobbsea3202812011-08-31 05:37:31 +0000444 char *uuid;
Jason Hobbsea3202812011-08-31 05:37:31 +0000445#endif
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200446 int clientarch = -1;
wdenk29e7f5a2004-03-12 00:14:09 +0000447
Jon Loeliger5336a762007-07-09 22:08:34 -0500448#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000449 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000450#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500451#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200452 char *hostname;
stroesee0aadfb2003-08-28 14:17:32 +0000453#endif
wdenk3861aa52002-09-27 23:19:37 +0000454
wdenk29e7f5a2004-03-12 00:14:09 +0000455 *e++ = 99; /* RFC1048 Magic Cookie */
456 *e++ = 130;
457 *e++ = 83;
458 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000459
wdenk29e7f5a2004-03-12 00:14:09 +0000460 *e++ = 53; /* DHCP Message Type */
461 *e++ = 1;
462 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000463
wdenk29e7f5a2004-03-12 00:14:09 +0000464 *e++ = 57; /* Maximum DHCP Message Size */
465 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000466 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
467 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000468
Joe Hershberger5874dec2015-04-08 01:41:01 -0500469 if (server_ip.s_addr) {
470 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000471
wdenk29e7f5a2004-03-12 00:14:09 +0000472 *e++ = 54; /* ServerID */
473 *e++ = 4;
474 *e++ = tmp >> 24;
475 *e++ = tmp >> 16;
476 *e++ = tmp >> 8;
477 *e++ = tmp & 0xff;
478 }
wdenk3861aa52002-09-27 23:19:37 +0000479
Joe Hershberger5874dec2015-04-08 01:41:01 -0500480 if (requested_ip.s_addr) {
481 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000482
wdenk29e7f5a2004-03-12 00:14:09 +0000483 *e++ = 50; /* Requested IP */
484 *e++ = 4;
485 *e++ = tmp >> 24;
486 *e++ = tmp >> 16;
487 *e++ = tmp >> 8;
488 *e++ = tmp & 0xff;
489 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500490#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Simon Glass64b723f2017-08-03 12:22:12 -0600491 hostname = env_get("hostname");
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000492 if (hostname) {
493 int hostnamelen = strlen(hostname);
wdenk29e7f5a2004-03-12 00:14:09 +0000494
495 *e++ = 12; /* Hostname */
496 *e++ = hostnamelen;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000497 memcpy(e, hostname, hostnamelen);
wdenk29e7f5a2004-03-12 00:14:09 +0000498 e += hostnamelen;
499 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000500#endif
501
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200502#ifdef CONFIG_BOOTP_PXE_CLIENTARCH
Jason Hobbsea3202812011-08-31 05:37:31 +0000503 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200504#endif
505
Simon Glass64b723f2017-08-03 12:22:12 -0600506 if (env_get("bootp_arch"))
Simon Glass22c34c22017-08-03 12:22:13 -0600507 clientarch = env_get_ulong("bootp_arch", 16, clientarch);
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200508
509 if (clientarch > 0) {
510 *e++ = 93; /* Client System Architecture */
511 *e++ = 2;
512 *e++ = (clientarch >> 8) & 0xff;
513 *e++ = clientarch & 0xff;
514 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000515
516 *e++ = 94; /* Client Network Interface Identifier */
517 *e++ = 3;
518 *e++ = 1; /* type field for UNDI */
519 *e++ = 0; /* major revision */
520 *e++ = 0; /* minor revision */
521
Alexander Graf3d5fc4e2016-05-12 15:51:45 +0200522#ifdef CONFIG_LIB_UUID
Simon Glass64b723f2017-08-03 12:22:12 -0600523 uuid = env_get("pxeuuid");
Jason Hobbsea3202812011-08-31 05:37:31 +0000524
525 if (uuid) {
526 if (uuid_str_valid(uuid)) {
527 *e++ = 97; /* Client Machine Identifier */
528 *e++ = 17;
529 *e++ = 0; /* type 0 - UUID */
530
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200531 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsea3202812011-08-31 05:37:31 +0000532 e += 16;
533 } else {
534 printf("Invalid pxeuuid: %s\n", uuid);
535 }
536 }
Ilya Yanok30876582012-09-17 10:26:25 +0000537#endif
Jason Hobbsea3202812011-08-31 05:37:31 +0000538
Alexander Grafea73a042016-05-06 21:01:02 +0200539 e = add_vci(e);
stroesee0aadfb2003-08-28 14:17:32 +0000540
Jon Loeliger5336a762007-07-09 22:08:34 -0500541#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000542 x = dhcp_vendorex_prep(e);
543 if (x)
wdenk29e7f5a2004-03-12 00:14:09 +0000544 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000545#endif
546
wdenk29e7f5a2004-03-12 00:14:09 +0000547 *e++ = 55; /* Parameter Request List */
548 cnt = e++; /* Pointer to count of requested items */
549 *cnt = 0;
Jon Loeliger5336a762007-07-09 22:08:34 -0500550#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000551 *e++ = 1; /* Subnet Mask */
552 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000553#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500554#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000555 *e++ = 2;
556 *cnt += 1;
557#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500558#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000559 *e++ = 3; /* Router Option */
560 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000561#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500562#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000563 *e++ = 6; /* DNS Server(s) */
564 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000565#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500566#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000567 *e++ = 12; /* Hostname */
568 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000569#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500570#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000571 *e++ = 13; /* Boot File Size */
572 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000573#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500574#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000575 *e++ = 17; /* Boot path */
576 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000577#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500578#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000579 *e++ = 40; /* NIS Domain name request */
580 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000581#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500582#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000583 *e++ = 42;
584 *cnt += 1;
585#endif
Jason Liu1c7dca52010-11-14 12:23:09 +0800586 /* no options, so back up to avoid sending an empty request list */
587 if (*cnt == 0)
588 e -= 2;
589
wdenk29e7f5a2004-03-12 00:14:09 +0000590 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000591
wdenk29e7f5a2004-03-12 00:14:09 +0000592 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000593#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glassa0978eb2011-02-02 15:03:28 -0800594 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk29e7f5a2004-03-12 00:14:09 +0000595 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000596#endif
597
wdenk29e7f5a2004-03-12 00:14:09 +0000598 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000599}
600
Jon Loeligera9807e52007-07-10 11:05:02 -0500601#else
wdenk3861aa52002-09-27 23:19:37 +0000602/*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000603 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000604 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500605static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000606{
wdenk29e7f5a2004-03-12 00:14:09 +0000607 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000608
wdenk29e7f5a2004-03-12 00:14:09 +0000609 *e++ = 99; /* RFC1048 Magic Cookie */
610 *e++ = 130;
611 *e++ = 83;
612 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000613
Jon Loeliger54f35c22007-07-09 17:45:14 -0500614#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000615 *e++ = 53; /* DHCP Message Type */
616 *e++ = 1;
617 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000618
wdenk29e7f5a2004-03-12 00:14:09 +0000619 *e++ = 57; /* Maximum DHCP Message Size */
620 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000621 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
622 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeligera9807e52007-07-10 11:05:02 -0500623#endif
wdenk3861aa52002-09-27 23:19:37 +0000624
Alexander Grafea73a042016-05-06 21:01:02 +0200625 add_vci(e);
Ilya Yanok30876582012-09-17 10:26:25 +0000626
Jon Loeliger5336a762007-07-09 22:08:34 -0500627#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000628 *e++ = 1; /* Subnet mask request */
629 *e++ = 4;
630 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000631#endif
632
Jon Loeliger5336a762007-07-09 22:08:34 -0500633#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000634 *e++ = 3; /* Default gateway request */
635 *e++ = 4;
636 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000637#endif
638
Jon Loeliger5336a762007-07-09 22:08:34 -0500639#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000640 *e++ = 6; /* Domain Name Server */
641 *e++ = 4;
642 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000643#endif
644
Jon Loeliger5336a762007-07-09 22:08:34 -0500645#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000646 *e++ = 12; /* Host name request */
647 *e++ = 32;
648 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000649#endif
650
Jon Loeliger5336a762007-07-09 22:08:34 -0500651#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000652 *e++ = 13; /* Boot file size */
653 *e++ = 2;
654 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000655#endif
656
Jon Loeliger5336a762007-07-09 22:08:34 -0500657#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000658 *e++ = 17; /* Boot path */
659 *e++ = 32;
660 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000661#endif
662
Jon Loeliger5336a762007-07-09 22:08:34 -0500663#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000664 *e++ = 40; /* NIS Domain name request */
665 *e++ = 32;
666 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000667#endif
Luuk Paulussen6380e012011-05-16 18:29:19 +0000668#if defined(CONFIG_BOOTP_NTPSERVER)
669 *e++ = 42;
670 *e++ = 4;
671 e += 4;
672#endif
wdenk3861aa52002-09-27 23:19:37 +0000673
wdenk29e7f5a2004-03-12 00:14:09 +0000674 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000675
Andre Renaud23298cd2016-05-05 07:28:08 -0600676 /*
677 * If nothing in list, remove it altogether. Some DHCP servers get
678 * upset by this minor faux pas and do not respond at all.
679 */
680 if (e == start + 3) {
681 printf("*** Warning: no DHCP options requested\n");
682 e -= 3;
683 }
684
wdenk29e7f5a2004-03-12 00:14:09 +0000685 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000686}
Jon Loeligera9807e52007-07-10 11:05:02 -0500687#endif
wdenk3861aa52002-09-27 23:19:37 +0000688
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500689void bootp_reset(void)
Stephen Warren69e1e352014-07-25 17:30:48 -0600690{
Thierry Reding8977cda2014-08-19 10:21:24 +0200691 bootp_num_ids = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500692 bootp_try = 0;
Stephen Warren69e1e352014-07-25 17:30:48 -0600693 bootp_start = get_timer(0);
Thierry Reding8977cda2014-08-19 10:21:24 +0200694 bootp_timeout = 250;
Stephen Warren69e1e352014-07-25 17:30:48 -0600695}
696
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500697void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000698{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000699 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500700 struct bootp_hdr *bp;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000701 int extlen, pktlen, iplen;
702 int eth_hdr_size;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000703#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek764c29d2014-07-11 11:39:37 +0200704 ulong rand_ms;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000705#endif
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500706 u32 bootp_id;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500707 struct in_addr zero_ip;
708 struct in_addr bcast_ip;
Alexandre Messier15971322016-02-01 17:08:57 -0500709 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000710
Simon Glass768cbf02011-12-10 11:08:06 +0000711 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger54f35c22007-07-09 17:45:14 -0500712#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000713 dhcp_state = INIT;
714#endif
715
Simon Glass64b723f2017-08-03 12:22:12 -0600716 ep = env_get("bootpretryperiod");
Alexandre Messier15971322016-02-01 17:08:57 -0500717 if (ep != NULL)
718 time_taken_max = simple_strtoul(ep, NULL, 10);
719 else
720 time_taken_max = TIMEOUT_MS;
721
wdenk3861aa52002-09-27 23:19:37 +0000722#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500723 if (bootp_try == 0)
Joe Hershberger797f2c52012-05-23 07:57:58 +0000724 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000725
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500726 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
727 rand_ms = rand() >> (22 - bootp_try);
Joe Hershberger797f2c52012-05-23 07:57:58 +0000728 else /* After 3rd BOOTP request max 8192 * 1ms */
729 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000730
Joe Hershberger797f2c52012-05-23 07:57:58 +0000731 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek764c29d2014-07-11 11:39:37 +0200732 mdelay(rand_ms);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000733
wdenk3861aa52002-09-27 23:19:37 +0000734#endif /* CONFIG_BOOTP_RANDOM_DELAY */
735
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500736 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500737 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000738 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000739
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500740 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000741 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000742
743 /*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000744 * Next line results in incorrect packet size being transmitted,
745 * resulting in errors in some DHCP servers, reporting missing bytes.
746 * Size must be set in packet header after extension length has been
747 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000748 * C. Hallinan, DS4.COM, Inc.
749 */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000750 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500751 sizeof (struct bootp_hdr)); */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000752 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000753 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000754
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500755 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000756 bp->bp_op = OP_BOOTREQUEST;
757 bp->bp_htype = HWT_ETHER;
758 bp->bp_hlen = HWL_ETHER;
759 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +0200760 /*
761 * according to RFC1542, should be 0 on first request, secs since
762 * first request otherwise
763 */
764 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500765 zero_ip.s_addr = 0;
766 net_write_ip(&bp->bp_ciaddr, zero_ip);
767 net_write_ip(&bp->bp_yiaddr, zero_ip);
768 net_write_ip(&bp->bp_siaddr, zero_ip);
769 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500770 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger290c8992015-04-08 01:41:02 -0500771 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000772
773 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500774#if defined(CONFIG_CMD_DHCP)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500775 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
776 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000777#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500778 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeligera9807e52007-07-10 11:05:02 -0500779#endif
wdenk3861aa52002-09-27 23:19:37 +0000780
781 /*
782 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200783 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000784 */
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500785 bootp_id = ((u32)net_ethaddr[2] << 24)
786 | ((u32)net_ethaddr[3] << 16)
787 | ((u32)net_ethaddr[4] << 8)
788 | (u32)net_ethaddr[5];
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500789 bootp_id += get_timer(0);
790 bootp_id = htonl(bootp_id);
791 bootp_add_id(bootp_id);
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500792 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000793
794 /*
795 * Calculate proper packet lengths taking into account the
796 * variable size of the options field
797 */
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000798 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
799 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500800 bcast_ip.s_addr = 0xFFFFFFFFL;
801 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500802 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000803
Jon Loeliger54f35c22007-07-09 17:45:14 -0500804#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000805 dhcp_state = SELECTING;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500806 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000807#else
Joe Hershberger5874dec2015-04-08 01:41:01 -0500808 net_set_udp_handler(bootp_handler);
Jon Loeligera9807e52007-07-10 11:05:02 -0500809#endif
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500810 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000811}
812
Jon Loeliger54f35c22007-07-09 17:45:14 -0500813#if defined(CONFIG_CMD_DHCP)
Stefan Brüns79f724c2015-09-04 00:31:49 +0200814static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000815{
wdenk3861aa52002-09-27 23:19:37 +0000816 int oplen, size;
Wolfgang Denk2b184222009-09-11 09:05:32 +0200817#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
818 int *to_ptr;
819#endif
wdenk3861aa52002-09-27 23:19:37 +0000820
wdenk29e7f5a2004-03-12 00:14:09 +0000821 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000822 oplen = *(popt + 1);
wdenk29e7f5a2004-03-12 00:14:09 +0000823 switch (*popt) {
Stefan Brüns44318292015-08-28 10:15:54 +0200824 case 0:
825 oplen = -1; /* Pad omits len byte */
826 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000827 case 1:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500828 net_copy_ip(&net_netmask, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000829 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500830#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000831 case 2: /* Time offset */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500832 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500833 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500834 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkb4ad9622005-04-01 00:25:43 +0000835 break;
836#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000837 case 3:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500838 net_copy_ip(&net_gateway, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000839 break;
840 case 6:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500841 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500842#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000843 if (*(popt + 1) > 4)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500844 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000845#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000846 break;
847 case 12:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000848 size = truncate_sz("Host Name",
Joe Hershberger6d236432015-04-08 01:41:03 -0500849 sizeof(net_hostname), oplen);
850 memcpy(&net_hostname, popt + 2, size);
851 net_hostname[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000852 break;
853 case 15: /* Ignore Domain Name Option */
854 break;
855 case 17:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000856 size = truncate_sz("Root Path",
Joe Hershberger6d236432015-04-08 01:41:03 -0500857 sizeof(net_root_path), oplen);
858 memcpy(&net_root_path, popt + 2, size);
859 net_root_path[size] = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000860 break;
Brian Rzycki5a672a22012-09-11 09:22:53 +0000861 case 28: /* Ignore Broadcast Address Option */
862 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500863#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000864 case 42: /* NTP server IP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500865 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkb4ad9622005-04-01 00:25:43 +0000866 break;
867#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000868 case 51:
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500869 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000870 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200871 case 52:
872 dhcp_option_overload = popt[2];
873 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000874 case 53: /* Ignore Message Type Option */
875 break;
876 case 54:
Joe Hershberger5874dec2015-04-08 01:41:01 -0500877 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000878 break;
879 case 58: /* Ignore Renewal Time Option */
880 break;
881 case 59: /* Ignore Rebinding Time Option */
882 break;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100883 case 66: /* Ignore TFTP server name */
884 break;
Stefan Brüns345cb832015-09-04 00:31:48 +0200885 case 67: /* Bootfile option */
886 size = truncate_sz("Bootfile",
887 sizeof(net_boot_file_name), oplen);
888 memcpy(&net_boot_file_name, popt + 2, size);
889 net_boot_file_name[size] = 0;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100890 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000891 default:
Jon Loeliger5336a762007-07-09 22:08:34 -0500892#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000893 if (dhcp_vendorex_proc(popt))
wdenk57b2d802003-06-27 21:31:46 +0000894 break;
wdenk3861aa52002-09-27 23:19:37 +0000895#endif
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000896 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500897 " %d\n", *popt);
wdenk29e7f5a2004-03-12 00:14:09 +0000898 break;
wdenk3861aa52002-09-27 23:19:37 +0000899 }
900 popt += oplen + 2; /* Process next option */
901 }
902}
903
Stefan Brüns79f724c2015-09-04 00:31:49 +0200904static void dhcp_packet_process_options(struct bootp_hdr *bp)
905{
906 uchar *popt = (uchar *)&bp->bp_vend[4];
907 uchar *end = popt + BOOTP_HDR_SIZE;
908
909 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
910 return;
911
912 dhcp_option_overload = 0;
913
914 /*
915 * The 'options' field MUST be interpreted first, 'file' next,
916 * 'sname' last.
917 */
918 dhcp_process_options(popt, end);
919
920 if (dhcp_option_overload & OVERLOAD_FILE) {
921 popt = (uchar *)bp->bp_file;
922 end = popt + sizeof(bp->bp_file);
923 dhcp_process_options(popt, end);
924 }
925
926 if (dhcp_option_overload & OVERLOAD_SNAME) {
927 popt = (uchar *)bp->bp_sname;
928 end = popt + sizeof(bp->bp_sname);
929 dhcp_process_options(popt, end);
930 }
931}
932
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500933static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +0000934{
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500935 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000936 return -1;
937
938 popt += 4;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000939 while (*popt != 0xff) {
940 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000941 return *(popt + 2);
Stefan Brüns44318292015-08-28 10:15:54 +0200942 if (*popt == 0) {
943 /* Pad */
944 popt += 1;
945 } else {
946 /* Scan through all options */
947 popt += *(popt + 1) + 2;
948 }
wdenk3861aa52002-09-27 23:19:37 +0000949 }
950 return -1;
951}
952
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500953static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000954{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000955 uchar *pkt, *iphdr;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500956 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000957 int pktlen, iplen, extlen;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000958 int eth_hdr_size;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500959 struct in_addr offered_ip;
960 struct in_addr zero_ip;
961 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000962
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500963 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500964 pkt = net_tx_packet;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000965 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000966
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500967 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000968 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000969
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000970 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000971 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000972
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500973 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000974 bp->bp_op = OP_BOOTREQUEST;
975 bp->bp_htype = HWT_ETHER;
976 bp->bp_hlen = HWL_ETHER;
977 bp->bp_hops = 0;
Stefan Brüns2eb25562015-08-27 23:57:18 +0200978 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000979 /* Do not set the client IP, your IP, or server IP yet, since it
980 * hasn't been ACK'ed by the server yet */
Justin Flammia01f256b2007-10-29 17:40:35 -0400981
Wolfgang Denk8a933fb2006-10-12 00:01:08 +0200982 /*
Wolfgang Denk68e83a82006-10-09 01:26:14 +0200983 * RFC3046 requires Relay Agents to discard packets with
984 * nonzero and offered giaddr
985 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500986 zero_ip.s_addr = 0;
987 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denk68e83a82006-10-09 01:26:14 +0200988
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500989 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierad28a312016-01-28 11:19:02 -0500990 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000991
992 /*
993 * ID is the id of the OFFER packet
994 */
995
Sergey Temerkhanovbf5ad642015-04-08 01:41:22 -0500996 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +0000997
998 /*
999 * Copy options from OFFER packet if present
1000 */
Justin Flammia01f256b2007-10-29 17:40:35 -04001001
1002 /* Copy offered IP into the parameters request list */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001003 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
1004 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
1005 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +00001006
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +00001007 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
1008 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001009 bcast_ip.s_addr = 0xFFFFFFFFL;
1010 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +00001011
Aras Vaichas72aa3f32008-03-26 09:43:57 +11001012#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
1013 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
1014#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerb1e94762012-05-23 07:59:11 +00001015 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001016 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +00001017}
1018
1019/*
1020 * Handle DHCP received packets.
1021 */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001022static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1023 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +00001024{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001025 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001026
Robin Getz9e0a4d62009-07-23 03:01:03 -04001027 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001028 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001029
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001030 /* Filter out pkts we don't want */
Stefan Brünsa48cd932015-08-27 23:53:26 +02001031 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001032 return;
1033
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001034 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1035 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001036
Peng Fan67e72e92016-01-07 15:28:23 +08001037 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
1038 return;
1039
wdenk3861aa52002-09-27 23:19:37 +00001040 switch (dhcp_state) {
1041 case SELECTING:
1042 /*
1043 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001044 * to arrive. Then select one, and generate DHCPREQUEST
1045 * response. If filename is in format we recognize, assume it
1046 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001047 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001048 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001049#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001050 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001051 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001052 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001053#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Stefan Brüns79f724c2015-09-04 00:31:49 +02001054 dhcp_packet_process_options(bp);
Alexander Graf94c4b992016-05-06 21:01:01 +02001055 efi_net_set_dhcp_ack(pkt, len);
wdenk3861aa52002-09-27 23:19:37 +00001056
Robin Getz9e0a4d62009-07-23 03:01:03 -04001057 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001058 dhcp_state = REQUESTING;
stroese5fa6e902003-04-10 13:26:44 +00001059
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001060 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001061 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001062#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001063 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001064#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001065
1066 return;
1067 break;
1068 case REQUESTING:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001069 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001070
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001071 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Stefan Brüns79f724c2015-09-04 00:31:49 +02001072 dhcp_packet_process_options(bp);
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001073 /* Store net params from reply */
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001074 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001075 dhcp_state = BOUND;
Thierry Reding8977cda2014-08-19 10:21:24 +02001076 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001077 &net_ip, get_timer(bootp_start));
Stefan Brüns4c60e4a2015-08-30 17:47:17 +02001078 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +00001079 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001080 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001081
Simon Glass5234ad12011-10-27 06:24:32 +00001082 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001083 return;
1084 }
1085 break;
Remy Bohmer03a44492008-08-20 11:30:28 +02001086 case BOUND:
1087 /* DHCP client bound to address */
1088 break;
wdenk3861aa52002-09-27 23:19:37 +00001089 default:
Joe Hershberger8f4b1352012-05-15 08:59:06 +00001090 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001091 break;
1092 }
wdenk3861aa52002-09-27 23:19:37 +00001093}
1094
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001095void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001096{
Joe Hershberger2fe81a52015-04-08 01:41:09 -05001097 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001098}
Wolfgang Denkf7a7f082007-11-03 23:09:27 +01001099#endif /* CONFIG_CMD_DHCP */