blob: aa6cdf0a47a5c17a36a711c62e72043890ce2465 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk232c1502004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
12#include <command.h>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020013#include <efi_loader.h>
wdenk3861aa52002-09-27 23:19:37 +000014#include <net.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020015#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000016#include "bootp.h"
wdenk232c1502004-03-12 00:14:09 +000017#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000018#ifdef CONFIG_STATUS_LED
19#include <status_led.h>
20#endif
Kim Phillipsdb7720b2012-07-05 13:19:32 +000021#ifdef CONFIG_BOOTP_RANDOM_DELAY
22#include "net_rand.h"
23#endif
wdenk3861aa52002-09-27 23:19:37 +000024
Joe Hershberger3090b7e32012-05-15 08:59:06 +000025#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000026
Stephen Warrenf59be6e2014-07-25 17:30:48 -060027/*
28 * The timeout for the initial BOOTP/DHCP request used to be described by a
29 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
30 * that counter
31 *
32 * Now that the timeout periods are variable (exponential backoff and retry)
33 * we convert the timeout count to the absolute time it would have take to
34 * execute that many retries, and keep sending retry packets until that time
35 * is reached.
36 */
wdenk232c1502004-03-12 00:14:09 +000037#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000038# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000039#else
wdenk232c1502004-03-12 00:14:09 +000040# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000041#endif
Stephen Warrenf59be6e2014-07-25 17:30:48 -060042#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000043
Joe Hershberger3090b7e32012-05-15 08:59:06 +000044#define PORT_BOOTPS 67 /* BOOTP server UDP port */
45#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000046
Joe Hershberger3090b7e32012-05-15 08:59:06 +000047#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000048#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000049#endif
50
Thierry Reding92ac8ac2014-08-19 10:21:24 +020051#ifndef CONFIG_BOOTP_ID_CACHE_SIZE
52#define CONFIG_BOOTP_ID_CACHE_SIZE 4
53#endif
54
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -050055u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
Thierry Reding92ac8ac2014-08-19 10:21:24 +020056unsigned int bootp_num_ids;
Joe Hershberger7044c6b2015-04-08 01:41:09 -050057int bootp_try;
Stephen Warrenf59be6e2014-07-25 17:30:48 -060058ulong bootp_start;
59ulong bootp_timeout;
Joe Hershberger586cbe52015-04-08 01:41:03 -050060char net_nis_domain[32] = {0,}; /* Our NIS domain */
61char net_hostname[32] = {0,}; /* Our hostname */
62char net_root_path[64] = {0,}; /* Our bootpath */
wdenk3861aa52002-09-27 23:19:37 +000063
Alexandre Messier50768f52016-02-01 17:08:57 -050064static ulong time_taken_max;
65
Jon Loeliger643d1ab2007-07-09 17:45:14 -050066#if defined(CONFIG_CMD_DHCP)
Kim Phillips06370592012-10-29 13:34:33 +000067static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -050068static u32 dhcp_leasetime;
Joe Hershberger049a95a2015-04-08 01:41:01 -050069static struct in_addr dhcp_server_ip;
Stefan Brünsec87b1b2015-09-04 00:31:48 +020070static u8 dhcp_option_overload;
71#define OVERLOAD_FILE 1
72#define OVERLOAD_SNAME 2
Joe Hershberger049a95a2015-04-08 01:41:01 -050073static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
74 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000075
76/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000077#if 0
78static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000079{
80 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000081 case 1: return "DHCPDISCOVER"; break;
82 case 2: return "DHCPOFFER"; break;
83 case 3: return "DHCPREQUEST"; break;
84 case 4: return "DHCPDECLINE"; break;
85 case 5: return "DHCPACK"; break;
86 case 6: return "DHCPNACK"; break;
87 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000088 default: return "UNKNOWN/INVALID MSG TYPE"; break;
89 }
90}
wdenk3e386912003-04-05 00:53:31 +000091#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050092#endif
wdenk3861aa52002-09-27 23:19:37 +000093
Thierry Reding92ac8ac2014-08-19 10:21:24 +020094static void bootp_add_id(ulong id)
95{
96 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
97 size_t size = sizeof(bootp_ids) - sizeof(id);
98
99 memmove(bootp_ids, &bootp_ids[1], size);
100 bootp_ids[bootp_num_ids - 1] = id;
101 } else {
102 bootp_ids[bootp_num_ids] = id;
103 bootp_num_ids++;
104 }
105}
106
107static bool bootp_match_id(ulong id)
108{
109 unsigned int i;
110
111 for (i = 0; i < bootp_num_ids; i++)
112 if (bootp_ids[i] == id)
113 return true;
114
115 return false;
116}
117
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200118static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
119 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000120{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500121 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000122 int retval = 0;
123
124 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
125 retval = -1;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500126 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000127 retval = -2;
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200128 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000129 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000130 else if (bp->bp_htype != HWT_ETHER)
131 retval = -4;
132 else if (bp->bp_hlen != HWL_ETHER)
133 retval = -5;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500134 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000135 retval = -6;
Anton Persson214cc902016-03-17 09:38:21 +0100136 else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0)
137 retval = -7;
wdenk3861aa52002-09-27 23:19:37 +0000138
Robin Getz0ebf04c2009-07-23 03:01:03 -0400139 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000140
141 return retval;
142}
143
144/*
145 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
146 */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500147static void store_net_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000148{
Wilson Callan5d110f02007-07-28 10:56:13 -0400149#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500150 struct in_addr tmp_ip;
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000151
Joe Hershberger049a95a2015-04-08 01:41:01 -0500152 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
153 if (tmp_ip.s_addr != 0)
154 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500155 memcpy(net_server_ethaddr,
156 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200157 if (
158#if defined(CONFIG_CMD_DHCP)
159 !(dhcp_option_overload & OVERLOAD_FILE) &&
160#endif
161 (strlen(bp->bp_file) > 0)) {
Joe Hershberger14111572015-04-08 01:41:02 -0500162 copy_filename(net_boot_file_name, bp->bp_file,
163 sizeof(net_boot_file_name));
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200164 }
wdenk3861aa52002-09-27 23:19:37 +0000165
Joe Hershberger14111572015-04-08 01:41:02 -0500166 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000167
168 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000169 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000170 * not contain a new value
171 */
Joe Hershberger14111572015-04-08 01:41:02 -0500172 if (*net_boot_file_name)
173 setenv("bootfile", net_boot_file_name);
Wu, Joshecec4e92014-11-18 13:07:08 +0800174#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500175 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000176}
177
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000178static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000179{
180 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000181 printf("*** WARNING: %s is too long (%d - max: %d)"
182 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000183 curlen = maxlen - 1;
184 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000185 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000186}
187
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500188#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000189
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500190static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000191{
wdenk232c1502004-03-12 00:14:09 +0000192 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000193
Robin Getz0ebf04c2009-07-23 03:01:03 -0400194 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500195 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000196
Joe Hershberger14111572015-04-08 01:41:02 -0500197 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000198
wdenk232c1502004-03-12 00:14:09 +0000199 switch (*ext) {
200 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000201 case 1: /* Subnet mask */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500202 if (net_netmask.s_addr == 0)
203 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000204 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000205 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000206 break;
wdenk232c1502004-03-12 00:14:09 +0000207 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000208 case 3: /* Gateways list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500209 if (net_gateway.s_addr == 0)
210 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000211 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000212 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000213 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000214 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000215 break;
216 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500217 if (net_dns_server.s_addr == 0)
218 net_copy_ip(&net_dns_server,
219 (struct in_addr *)(ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500220#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500221 if ((net_dns_server2.s_addr == 0) && (size > 4))
222 net_copy_ip(&net_dns_server2,
223 (struct in_addr *)(ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000224#endif
wdenk3861aa52002-09-27 23:19:37 +0000225 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000226 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000227 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000228 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000230 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000231 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000232 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000233 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000234 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000235 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000236 case 12: /* Host name */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500237 if (net_hostname[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000238 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500239 sizeof(net_hostname), size);
240 memcpy(&net_hostname, ext + 2, size);
241 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000242 }
243 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000244 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000245 if (size == 2)
Joe Hershberger14111572015-04-08 01:41:02 -0500246 net_boot_file_expected_size_in_blocks =
247 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000248 else if (size == 4)
Joe Hershberger14111572015-04-08 01:41:02 -0500249 net_boot_file_expected_size_in_blocks =
250 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000251 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000252 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000253 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000254 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000255 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000256 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000257 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000258 case 17: /* Root path */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500259 if (net_root_path[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000260 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500261 sizeof(net_root_path), size);
262 memcpy(&net_root_path, ext + 2, size);
263 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000264 }
265 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000266 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000267 /*
wdenk8bde7f72003-06-27 21:31:46 +0000268 * This can be used to send the information of the
269 * vendor area in another file that the client can
270 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000271 */
272 break;
wdenk232c1502004-03-12 00:14:09 +0000273 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000274 case 40: /* NIS Domain name */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500275 if (net_nis_domain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000276 size = truncate_sz("NIS Domain Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500277 sizeof(net_nis_domain), size);
278 memcpy(&net_nis_domain, ext + 2, size);
279 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000280 }
281 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000282#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
283 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500284 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000285 break;
286#endif
wdenk232c1502004-03-12 00:14:09 +0000287 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000288 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000289 /*
wdenk8bde7f72003-06-27 21:31:46 +0000290 * Binary information to exchange specific
291 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000292 */
293 break;
wdenk232c1502004-03-12 00:14:09 +0000294 /* Reserved (custom) fields (128..254) */
295 }
wdenk3861aa52002-09-27 23:19:37 +0000296}
297
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500298static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000299{
wdenk232c1502004-03-12 00:14:09 +0000300 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000301
Robin Getz0ebf04c2009-07-23 03:01:03 -0400302 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000303
wdenk232c1502004-03-12 00:14:09 +0000304 while ((ext < end) && (*ext != 0xff)) {
305 if (*ext == 0) {
306 ext++;
307 } else {
308 u8 *opt = ext;
309
310 ext += ext[1] + 2;
311 if (ext <= end)
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500312 bootp_process_vendor_field(opt);
wdenk232c1502004-03-12 00:14:09 +0000313 }
wdenk3861aa52002-09-27 23:19:37 +0000314 }
wdenk3861aa52002-09-27 23:19:37 +0000315
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000316 debug("[BOOTP] Received fields:\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500317 if (net_netmask.s_addr)
318 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000319
Joe Hershberger049a95a2015-04-08 01:41:01 -0500320 if (net_gateway.s_addr)
321 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000322
Joe Hershberger14111572015-04-08 01:41:02 -0500323 if (net_boot_file_expected_size_in_blocks)
324 debug("net_boot_file_expected_size_in_blocks : %d\n",
325 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000326
Joe Hershberger586cbe52015-04-08 01:41:03 -0500327 if (net_hostname[0])
328 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000329
Joe Hershberger586cbe52015-04-08 01:41:03 -0500330 if (net_root_path[0])
331 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000332
Joe Hershberger586cbe52015-04-08 01:41:03 -0500333 if (net_nis_domain[0])
334 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000335
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000336#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500337 if (net_ntp_server)
338 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000339#endif
wdenk3861aa52002-09-27 23:19:37 +0000340}
Simon Glass09349862011-06-13 16:13:12 -0700341
wdenk3861aa52002-09-27 23:19:37 +0000342/*
343 * Handle a BOOTP received packet.
344 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500345static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
346 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000347{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500348 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000349
Robin Getz0ebf04c2009-07-23 03:01:03 -0400350 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500351 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000352
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500353 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000354
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000355 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200356 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000357 return;
358
359 /*
wdenk232c1502004-03-12 00:14:09 +0000360 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000361 */
Thomas Chou82953322015-08-25 20:54:24 +0800362#if defined(CONFIG_STATUS_LED) && defined(STATUS_LED_BOOT)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000363 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000364#endif
365
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500366 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000367
368 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500369 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500370 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000371
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500372 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000373 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000374
Robin Getz0ebf04c2009-07-23 03:01:03 -0400375 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000376
Simon Glasse4a3d572011-10-27 06:24:32 +0000377 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000378}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500379#endif
wdenk3861aa52002-09-27 23:19:37 +0000380
381/*
382 * Timeout on BOOTP/DHCP request.
383 */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500384static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000385{
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600386 ulong time_taken = get_timer(bootp_start);
387
Alexandre Messier50768f52016-02-01 17:08:57 -0500388 if (time_taken >= time_taken_max) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000389#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600390 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000391 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000392#else
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600393 puts("\nRetry time exceeded; starting again\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500394 net_start_again();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000395#endif
wdenk3861aa52002-09-27 23:19:37 +0000396 } else {
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600397 bootp_timeout *= 2;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200398 if (bootp_timeout > 2000)
399 bootp_timeout = 2000;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500400 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500401 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000402 }
403}
404
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000405#define put_vci(e, str) \
406 do { \
407 size_t vci_strlen = strlen(str); \
408 *e++ = 60; /* Vendor Class Identifier */ \
409 *e++ = vci_strlen; \
410 memcpy(e, str, vci_strlen); \
411 e += vci_strlen; \
412 } while (0)
413
Alexander Graf4570a992016-05-06 21:01:02 +0200414static u8 *add_vci(u8 *e)
415{
Alexander Graf20898ea2016-05-06 21:01:07 +0200416 char *vci = NULL;
417 char *env_vci = getenv("bootp_vci");
418
Alexander Graf4570a992016-05-06 21:01:02 +0200419#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
Alexander Graf20898ea2016-05-06 21:01:07 +0200420 vci = CONFIG_SPL_NET_VCI_STRING;
Alexander Graf4570a992016-05-06 21:01:02 +0200421#elif defined(CONFIG_BOOTP_VCI_STRING)
Alexander Graf20898ea2016-05-06 21:01:07 +0200422 vci = CONFIG_BOOTP_VCI_STRING;
Alexander Graf4570a992016-05-06 21:01:02 +0200423#endif
424
Alexander Graf20898ea2016-05-06 21:01:07 +0200425 if (env_vci)
426 vci = env_vci;
427
428 if (vci)
429 put_vci(e, vci);
430
Alexander Graf4570a992016-05-06 21:01:02 +0200431 return e;
432}
433
wdenk3861aa52002-09-27 23:19:37 +0000434/*
435 * Initialize BOOTP extension fields in the request.
436 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500437#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500438static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
439 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000440{
wdenk232c1502004-03-12 00:14:09 +0000441 u8 *start = e;
442 u8 *cnt;
Alexander Grafbc6fc282016-05-12 15:51:45 +0200443#ifdef CONFIG_LIB_UUID
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000444 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000445#endif
Alexander Grafbc6fc282016-05-12 15:51:45 +0200446 int clientarch = -1;
wdenk232c1502004-03-12 00:14:09 +0000447
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500448#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000449 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000450#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500451#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200452 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000453#endif
wdenk3861aa52002-09-27 23:19:37 +0000454
wdenk232c1502004-03-12 00:14:09 +0000455 *e++ = 99; /* RFC1048 Magic Cookie */
456 *e++ = 130;
457 *e++ = 83;
458 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000459
wdenk232c1502004-03-12 00:14:09 +0000460 *e++ = 53; /* DHCP Message Type */
461 *e++ = 1;
462 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000463
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 57; /* Maximum DHCP Message Size */
465 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000466 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
467 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000468
Joe Hershberger049a95a2015-04-08 01:41:01 -0500469 if (server_ip.s_addr) {
470 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000471
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 54; /* ServerID */
473 *e++ = 4;
474 *e++ = tmp >> 24;
475 *e++ = tmp >> 16;
476 *e++ = tmp >> 8;
477 *e++ = tmp & 0xff;
478 }
wdenk3861aa52002-09-27 23:19:37 +0000479
Joe Hershberger049a95a2015-04-08 01:41:01 -0500480 if (requested_ip.s_addr) {
481 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000482
wdenk232c1502004-03-12 00:14:09 +0000483 *e++ = 50; /* Requested IP */
484 *e++ = 4;
485 *e++ = tmp >> 24;
486 *e++ = tmp >> 16;
487 *e++ = tmp >> 8;
488 *e++ = tmp & 0xff;
489 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500490#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000491 hostname = getenv("hostname");
492 if (hostname) {
493 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000494
495 *e++ = 12; /* Hostname */
496 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000497 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000498 e += hostnamelen;
499 }
stroesefe389a82003-08-28 14:17:32 +0000500#endif
501
Alexander Grafbc6fc282016-05-12 15:51:45 +0200502#ifdef CONFIG_BOOTP_PXE_CLIENTARCH
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000503 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
Alexander Grafbc6fc282016-05-12 15:51:45 +0200504#endif
505
506 if (getenv("bootp_arch"))
507 clientarch = getenv_ulong("bootp_arch", 16, clientarch);
508
509 if (clientarch > 0) {
510 *e++ = 93; /* Client System Architecture */
511 *e++ = 2;
512 *e++ = (clientarch >> 8) & 0xff;
513 *e++ = clientarch & 0xff;
514 }
Jason Hobbsd2b5d5c2011-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 Grafbc6fc282016-05-12 15:51:45 +0200522#ifdef CONFIG_LIB_UUID
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000523 uuid = getenv("pxeuuid");
524
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 Marczakd718ded2014-04-02 10:20:03 +0200531 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000532 e += 16;
533 } else {
534 printf("Invalid pxeuuid: %s\n", uuid);
535 }
536 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000537#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000538
Alexander Graf4570a992016-05-06 21:01:02 +0200539 e = add_vci(e);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000540
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500541#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000542 x = dhcp_vendorex_prep(e);
543 if (x)
wdenk232c1502004-03-12 00:14:09 +0000544 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000545#endif
546
wdenk232c1502004-03-12 00:14:09 +0000547 *e++ = 55; /* Parameter Request List */
548 cnt = e++; /* Pointer to count of requested items */
549 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500550#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000551 *e++ = 1; /* Subnet Mask */
552 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000553#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500554#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000555 *e++ = 2;
556 *cnt += 1;
557#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500558#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000559 *e++ = 3; /* Router Option */
560 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000561#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500562#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000563 *e++ = 6; /* DNS Server(s) */
564 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000565#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500566#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000567 *e++ = 12; /* Hostname */
568 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000569#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500570#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000571 *e++ = 13; /* Boot File Size */
572 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000573#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500574#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000575 *e++ = 17; /* Boot path */
576 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000577#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500578#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000579 *e++ = 40; /* NIS Domain name request */
580 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000581#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500582#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000583 *e++ = 42;
584 *cnt += 1;
585#endif
Jason Liu258ccd62010-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
wdenk232c1502004-03-12 00:14:09 +0000590 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000591
wdenk232c1502004-03-12 00:14:09 +0000592 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000593#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800594 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000595 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000596#endif
597
wdenk232c1502004-03-12 00:14:09 +0000598 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000599}
600
Jon Loeliger610f2e92007-07-10 11:05:02 -0500601#else
wdenk3861aa52002-09-27 23:19:37 +0000602/*
Joe Hershberger3090b7e32012-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 Hershberger049a95a2015-04-08 01:41:01 -0500605static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000606{
wdenk232c1502004-03-12 00:14:09 +0000607 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000608
wdenk232c1502004-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 Loeliger643d1ab2007-07-09 17:45:14 -0500614#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-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
wdenk232c1502004-03-12 00:14:09 +0000619 *e++ = 57; /* Maximum DHCP Message Size */
620 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000621 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
622 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500623#endif
wdenk3861aa52002-09-27 23:19:37 +0000624
Alexander Graf4570a992016-05-06 21:01:02 +0200625 add_vci(e);
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000626
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500627#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500633#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500639#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500645#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500651#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500657#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500663#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-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 Paulussen09e3a672011-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
wdenk232c1502004-03-12 00:14:09 +0000674 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000675
wdenk232c1502004-03-12 00:14:09 +0000676 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000677}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500678#endif
wdenk3861aa52002-09-27 23:19:37 +0000679
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500680void bootp_reset(void)
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600681{
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200682 bootp_num_ids = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500683 bootp_try = 0;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600684 bootp_start = get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200685 bootp_timeout = 250;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600686}
687
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500688void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000689{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000690 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500691 struct bootp_hdr *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000692 int extlen, pktlen, iplen;
693 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000694#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200695 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000696#endif
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500697 u32 bootp_id;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500698 struct in_addr zero_ip;
699 struct in_addr bcast_ip;
Alexandre Messier50768f52016-02-01 17:08:57 -0500700 char *ep; /* Environment pointer */
wdenk3861aa52002-09-27 23:19:37 +0000701
Simon Glass573f14f2011-12-10 11:08:06 +0000702 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500703#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000704 dhcp_state = INIT;
705#endif
706
Alexandre Messier50768f52016-02-01 17:08:57 -0500707 ep = getenv("bootpretryperiod");
708 if (ep != NULL)
709 time_taken_max = simple_strtoul(ep, NULL, 10);
710 else
711 time_taken_max = TIMEOUT_MS;
712
wdenk3861aa52002-09-27 23:19:37 +0000713#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500714 if (bootp_try == 0)
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000715 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000716
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500717 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
718 rand_ms = rand() >> (22 - bootp_try);
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000719 else /* After 3rd BOOTP request max 8192 * 1ms */
720 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000721
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000722 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200723 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000724
wdenk3861aa52002-09-27 23:19:37 +0000725#endif /* CONFIG_BOOTP_RANDOM_DELAY */
726
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500727 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500728 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000729 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000730
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500731 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000732 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000733
734 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000735 * Next line results in incorrect packet size being transmitted,
736 * resulting in errors in some DHCP servers, reporting missing bytes.
737 * Size must be set in packet header after extension length has been
738 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000739 * C. Hallinan, DS4.COM, Inc.
740 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000741 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500742 sizeof (struct bootp_hdr)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000743 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000744 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000745
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500746 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000747 bp->bp_op = OP_BOOTREQUEST;
748 bp->bp_htype = HWT_ETHER;
749 bp->bp_hlen = HWL_ETHER;
750 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200751 /*
752 * according to RFC1542, should be 0 on first request, secs since
753 * first request otherwise
754 */
755 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500756 zero_ip.s_addr = 0;
757 net_write_ip(&bp->bp_ciaddr, zero_ip);
758 net_write_ip(&bp->bp_yiaddr, zero_ip);
759 net_write_ip(&bp->bp_siaddr, zero_ip);
760 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500761 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger14111572015-04-08 01:41:02 -0500762 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000763
764 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500765#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500766 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
767 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000768#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500769 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500770#endif
wdenk3861aa52002-09-27 23:19:37 +0000771
772 /*
773 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200774 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000775 */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500776 bootp_id = ((u32)net_ethaddr[2] << 24)
777 | ((u32)net_ethaddr[3] << 16)
778 | ((u32)net_ethaddr[4] << 8)
779 | (u32)net_ethaddr[5];
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500780 bootp_id += get_timer(0);
781 bootp_id = htonl(bootp_id);
782 bootp_add_id(bootp_id);
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500783 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000784
785 /*
786 * Calculate proper packet lengths taking into account the
787 * variable size of the options field
788 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000789 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
790 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500791 bcast_ip.s_addr = 0xFFFFFFFFL;
792 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500793 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000794
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500795#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000796 dhcp_state = SELECTING;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500797 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000798#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500799 net_set_udp_handler(bootp_handler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500800#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500801 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000802}
803
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500804#if defined(CONFIG_CMD_DHCP)
Stefan Brüns774c3e02015-09-04 00:31:49 +0200805static void dhcp_process_options(uchar *popt, uchar *end)
wdenk3861aa52002-09-27 23:19:37 +0000806{
wdenk3861aa52002-09-27 23:19:37 +0000807 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200808#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
809 int *to_ptr;
810#endif
wdenk3861aa52002-09-27 23:19:37 +0000811
wdenk232c1502004-03-12 00:14:09 +0000812 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000813 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000814 switch (*popt) {
Stefan Brünsc56eb572015-08-28 10:15:54 +0200815 case 0:
816 oplen = -1; /* Pad omits len byte */
817 break;
wdenk232c1502004-03-12 00:14:09 +0000818 case 1:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500819 net_copy_ip(&net_netmask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000820 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500821#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000822 case 2: /* Time offset */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500823 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500824 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500825 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkea287de2005-04-01 00:25:43 +0000826 break;
827#endif
wdenk232c1502004-03-12 00:14:09 +0000828 case 3:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500829 net_copy_ip(&net_gateway, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000830 break;
831 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500832 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500833#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000834 if (*(popt + 1) > 4)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500835 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000836#endif
wdenk232c1502004-03-12 00:14:09 +0000837 break;
838 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000839 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500840 sizeof(net_hostname), oplen);
841 memcpy(&net_hostname, popt + 2, size);
842 net_hostname[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000843 break;
844 case 15: /* Ignore Domain Name Option */
845 break;
846 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000847 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500848 sizeof(net_root_path), oplen);
849 memcpy(&net_root_path, popt + 2, size);
850 net_root_path[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000851 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000852 case 28: /* Ignore Broadcast Address Option */
853 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500854#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000855 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500856 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000857 break;
858#endif
wdenk232c1502004-03-12 00:14:09 +0000859 case 51:
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500860 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000861 break;
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200862 case 52:
863 dhcp_option_overload = popt[2];
864 break;
wdenk232c1502004-03-12 00:14:09 +0000865 case 53: /* Ignore Message Type Option */
866 break;
867 case 54:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500868 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000869 break;
870 case 58: /* Ignore Renewal Time Option */
871 break;
872 case 59: /* Ignore Rebinding Time Option */
873 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100874 case 66: /* Ignore TFTP server name */
875 break;
Stefan Brünsec87b1b2015-09-04 00:31:48 +0200876 case 67: /* Bootfile option */
877 size = truncate_sz("Bootfile",
878 sizeof(net_boot_file_name), oplen);
879 memcpy(&net_boot_file_name, popt + 2, size);
880 net_boot_file_name[size] = 0;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100881 break;
wdenk232c1502004-03-12 00:14:09 +0000882 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500883#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000884 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000885 break;
wdenk3861aa52002-09-27 23:19:37 +0000886#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000887 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500888 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000889 break;
wdenk3861aa52002-09-27 23:19:37 +0000890 }
891 popt += oplen + 2; /* Process next option */
892 }
893}
894
Stefan Brüns774c3e02015-09-04 00:31:49 +0200895static void dhcp_packet_process_options(struct bootp_hdr *bp)
896{
897 uchar *popt = (uchar *)&bp->bp_vend[4];
898 uchar *end = popt + BOOTP_HDR_SIZE;
899
900 if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
901 return;
902
903 dhcp_option_overload = 0;
904
905 /*
906 * The 'options' field MUST be interpreted first, 'file' next,
907 * 'sname' last.
908 */
909 dhcp_process_options(popt, end);
910
911 if (dhcp_option_overload & OVERLOAD_FILE) {
912 popt = (uchar *)bp->bp_file;
913 end = popt + sizeof(bp->bp_file);
914 dhcp_process_options(popt, end);
915 }
916
917 if (dhcp_option_overload & OVERLOAD_SNAME) {
918 popt = (uchar *)bp->bp_sname;
919 end = popt + sizeof(bp->bp_sname);
920 dhcp_process_options(popt, end);
921 }
922}
923
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500924static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +0000925{
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500926 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000927 return -1;
928
929 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000930 while (*popt != 0xff) {
931 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000932 return *(popt + 2);
Stefan Brünsc56eb572015-08-28 10:15:54 +0200933 if (*popt == 0) {
934 /* Pad */
935 popt += 1;
936 } else {
937 /* Scan through all options */
938 popt += *(popt + 1) + 2;
939 }
wdenk3861aa52002-09-27 23:19:37 +0000940 }
941 return -1;
942}
943
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500944static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000945{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000946 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500947 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000948 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000949 int eth_hdr_size;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500950 struct in_addr offered_ip;
951 struct in_addr zero_ip;
952 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000953
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500954 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500955 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000956 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000957
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500958 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000959 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000960
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000961 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000962 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000963
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500964 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000965 bp->bp_op = OP_BOOTREQUEST;
966 bp->bp_htype = HWT_ETHER;
967 bp->bp_hlen = HWL_ETHER;
968 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200969 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000970 /* Do not set the client IP, your IP, or server IP yet, since it
971 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400972
Wolfgang Denkc6686702006-10-12 00:01:08 +0200973 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200974 * RFC3046 requires Relay Agents to discard packets with
975 * nonzero and offered giaddr
976 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500977 zero_ip.s_addr = 0;
978 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200979
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500980 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Alexandre Messierb2b7fbc2016-01-28 11:19:02 -0500981 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000982
983 /*
984 * ID is the id of the OFFER packet
985 */
986
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500987 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +0000988
989 /*
990 * Copy options from OFFER packet if present
991 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400992
993 /* Copy offered IP into the parameters request list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500994 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
995 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
996 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +0000997
Joe Hershbergerae446f52012-05-23 07:59:10 +0000998 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
999 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -05001000 bcast_ip.s_addr = 0xFFFFFFFFL;
1001 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +00001002
Aras Vaichasd9a2f412008-03-26 09:43:57 +11001003#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
1004 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
1005#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +00001006 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001007 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +00001008}
1009
1010/*
1011 * Handle DHCP received packets.
1012 */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001013static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
1014 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +00001015{
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001016 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +00001017
Robin Getz0ebf04c2009-07-23 03:01:03 -04001018 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001019 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001020
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001021 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +02001022 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +00001023 return;
1024
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001025 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
1026 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +00001027
Peng Fan44c42dd2016-01-07 15:28:23 +08001028 if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
1029 return;
1030
wdenk3861aa52002-09-27 23:19:37 +00001031 switch (dhcp_state) {
1032 case SELECTING:
1033 /*
1034 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001035 * to arrive. Then select one, and generate DHCPREQUEST
1036 * response. If filename is in format we recognize, assume it
1037 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +00001038 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001039 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001040#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001041 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001042 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001043 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001044#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
Stefan Brüns774c3e02015-09-04 00:31:49 +02001045 dhcp_packet_process_options(bp);
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001046 efi_net_set_dhcp_ack(pkt, len);
wdenk3861aa52002-09-27 23:19:37 +00001047
Robin Getz0ebf04c2009-07-23 03:01:03 -04001048 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001049 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +00001050
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001051 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001052 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001053#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +00001054 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001055#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +00001056
1057 return;
1058 break;
1059 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001060 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001061
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001062 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Stefan Brüns774c3e02015-09-04 00:31:49 +02001063 dhcp_packet_process_options(bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001064 /* Store net params from reply */
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001065 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001066 dhcp_state = BOUND;
Thierry Reding92ac8ac2014-08-19 10:21:24 +02001067 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001068 &net_ip, get_timer(bootp_start));
Stefan Brüns4f28c9b2015-08-30 17:47:17 +02001069 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +00001070 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001071 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001072
Simon Glasse4a3d572011-10-27 06:24:32 +00001073 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001074 return;
1075 }
1076 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +02001077 case BOUND:
1078 /* DHCP client bound to address */
1079 break;
wdenk3861aa52002-09-27 23:19:37 +00001080 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001081 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001082 break;
1083 }
wdenk3861aa52002-09-27 23:19:37 +00001084}
1085
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001086void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001087{
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001088 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001089}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001090#endif /* CONFIG_CMD_DHCP */