blob: a56ed4cf8c0dea950a7a431f294d08114dec5233 [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>
13#include <net.h>
14#include "bootp.h"
15#include "tftp.h"
wdenk232c1502004-03-12 00:14:09 +000016#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000017#ifdef CONFIG_STATUS_LED
18#include <status_led.h>
19#endif
Kim Phillipsdb7720b2012-07-05 13:19:32 +000020#ifdef CONFIG_BOOTP_RANDOM_DELAY
21#include "net_rand.h"
22#endif
wdenk3861aa52002-09-27 23:19:37 +000023
Joe Hershberger3090b7e32012-05-15 08:59:06 +000024#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000025
Stephen Warrenf59be6e2014-07-25 17:30:48 -060026/*
27 * The timeout for the initial BOOTP/DHCP request used to be described by a
28 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
29 * that counter
30 *
31 * Now that the timeout periods are variable (exponential backoff and retry)
32 * we convert the timeout count to the absolute time it would have take to
33 * execute that many retries, and keep sending retry packets until that time
34 * is reached.
35 */
wdenk232c1502004-03-12 00:14:09 +000036#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000037# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000038#else
wdenk232c1502004-03-12 00:14:09 +000039# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000040#endif
Stephen Warrenf59be6e2014-07-25 17:30:48 -060041#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000042
Joe Hershberger3090b7e32012-05-15 08:59:06 +000043#define PORT_BOOTPS 67 /* BOOTP server UDP port */
44#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000045
Joe Hershberger3090b7e32012-05-15 08:59:06 +000046#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000047#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000048#endif
49
Thierry Reding92ac8ac2014-08-19 10:21:24 +020050#ifndef CONFIG_BOOTP_ID_CACHE_SIZE
51#define CONFIG_BOOTP_ID_CACHE_SIZE 4
52#endif
53
54ulong bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
55unsigned int bootp_num_ids;
wdenk3861aa52002-09-27 23:19:37 +000056int BootpTry;
Stephen Warrenf59be6e2014-07-25 17:30:48 -060057ulong bootp_start;
58ulong bootp_timeout;
wdenk3861aa52002-09-27 23:19:37 +000059
Jon Loeliger643d1ab2007-07-09 17:45:14 -050060#if defined(CONFIG_CMD_DHCP)
Kim Phillips06370592012-10-29 13:34:33 +000061static dhcp_state_t dhcp_state = INIT;
62static unsigned long dhcp_leasetime;
Joe Hershberger049a95a2015-04-08 01:41:01 -050063static struct in_addr dhcp_server_ip;
64static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
65 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000066
67/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000068#if 0
69static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000070{
71 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000072 case 1: return "DHCPDISCOVER"; break;
73 case 2: return "DHCPOFFER"; break;
74 case 3: return "DHCPREQUEST"; break;
75 case 4: return "DHCPDECLINE"; break;
76 case 5: return "DHCPACK"; break;
77 case 6: return "DHCPNACK"; break;
78 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000079 default: return "UNKNOWN/INVALID MSG TYPE"; break;
80 }
81}
wdenk3e386912003-04-05 00:53:31 +000082#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050083#endif
wdenk3861aa52002-09-27 23:19:37 +000084
Thierry Reding92ac8ac2014-08-19 10:21:24 +020085static void bootp_add_id(ulong id)
86{
87 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
88 size_t size = sizeof(bootp_ids) - sizeof(id);
89
90 memmove(bootp_ids, &bootp_ids[1], size);
91 bootp_ids[bootp_num_ids - 1] = id;
92 } else {
93 bootp_ids[bootp_num_ids] = id;
94 bootp_num_ids++;
95 }
96}
97
98static bool bootp_match_id(ulong id)
99{
100 unsigned int i;
101
102 for (i = 0; i < bootp_num_ids; i++)
103 if (bootp_ids[i] == id)
104 return true;
105
106 return false;
107}
108
wdenk3861aa52002-09-27 23:19:37 +0000109static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
110{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000111 struct Bootp_t *bp = (struct Bootp_t *) pkt;
wdenk3861aa52002-09-27 23:19:37 +0000112 int retval = 0;
113
114 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
115 retval = -1;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000116 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000117 retval = -2;
118 else if (bp->bp_op != OP_BOOTREQUEST &&
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000119 bp->bp_op != OP_BOOTREPLY &&
120 bp->bp_op != DHCP_OFFER &&
121 bp->bp_op != DHCP_ACK &&
122 bp->bp_op != DHCP_NAK)
wdenk3861aa52002-09-27 23:19:37 +0000123 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000124 else if (bp->bp_htype != HWT_ETHER)
125 retval = -4;
126 else if (bp->bp_hlen != HWL_ETHER)
127 retval = -5;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200128 else if (!bootp_match_id(NetReadLong((ulong *)&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000129 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +0000130
Robin Getz0ebf04c2009-07-23 03:01:03 -0400131 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000132
133 return retval;
134}
135
136/*
137 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
138 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000139static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000140{
Wilson Callan5d110f02007-07-28 10:56:13 -0400141#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500142 struct in_addr tmp_ip;
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000143
Joe Hershberger049a95a2015-04-08 01:41:01 -0500144 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
145 if (tmp_ip.s_addr != 0)
146 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershbergercb487f52012-05-23 07:58:06 +0000147 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
wdenk3d3befa2004-03-14 15:06:13 +0000148 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000149 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000150
Robin Getz0ebf04c2009-07-23 03:01:03 -0400151 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000152
153 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000154 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000155 * not contain a new value
156 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000157 if (*BootFile)
158 setenv("bootfile", BootFile);
Wu, Joshecec4e92014-11-18 13:07:08 +0800159#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500160 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000161}
162
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000163static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000164{
165 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000166 printf("*** WARNING: %s is too long (%d - max: %d)"
167 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000168 curlen = maxlen - 1;
169 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000170 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000171}
172
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500173#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000174
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000175static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000176{
wdenk232c1502004-03-12 00:14:09 +0000177 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000178
Robin Getz0ebf04c2009-07-23 03:01:03 -0400179 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000180 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000181
wdenk232c1502004-03-12 00:14:09 +0000182 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000183
wdenk232c1502004-03-12 00:14:09 +0000184 switch (*ext) {
185 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000186 case 1: /* Subnet mask */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500187 if (net_netmask.s_addr == 0)
188 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000189 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000190 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
wdenk232c1502004-03-12 00:14:09 +0000192 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000193 case 3: /* Gateways list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500194 if (net_gateway.s_addr == 0)
195 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000196 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000197 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000198 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000199 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000200 break;
201 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500202 if (net_dns_server.s_addr == 0)
203 net_copy_ip(&net_dns_server,
204 (struct in_addr *)(ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500205#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500206 if ((net_dns_server2.s_addr == 0) && (size > 4))
207 net_copy_ip(&net_dns_server2,
208 (struct in_addr *)(ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000209#endif
wdenk3861aa52002-09-27 23:19:37 +0000210 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000211 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000212 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000213 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000214 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000215 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000216 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000217 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000218 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000219 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000220 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000221 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000222 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000223 size = truncate_sz("Host Name",
224 sizeof(NetOurHostName), size);
225 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000226 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000227 }
228 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000229 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000230 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000231 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000232 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000233 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000234 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000235 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000236 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000237 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000238 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000239 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000240 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000241 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000242 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000243 size = truncate_sz("Root Path",
244 sizeof(NetOurRootPath), size);
245 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000246 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000247 }
248 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000249 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000250 /*
wdenk8bde7f72003-06-27 21:31:46 +0000251 * This can be used to send the information of the
252 * vendor area in another file that the client can
253 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000254 */
255 break;
wdenk232c1502004-03-12 00:14:09 +0000256 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000257 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000258 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000259 size = truncate_sz("NIS Domain Name",
260 sizeof(NetOurNISDomain), size);
261 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000262 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000263 }
264 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000265#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
266 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500267 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000268 break;
269#endif
wdenk232c1502004-03-12 00:14:09 +0000270 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000271 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000272 /*
wdenk8bde7f72003-06-27 21:31:46 +0000273 * Binary information to exchange specific
274 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000275 */
276 break;
wdenk232c1502004-03-12 00:14:09 +0000277 /* Reserved (custom) fields (128..254) */
278 }
wdenk3861aa52002-09-27 23:19:37 +0000279}
280
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000281static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000282{
wdenk232c1502004-03-12 00:14:09 +0000283 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000284
Robin Getz0ebf04c2009-07-23 03:01:03 -0400285 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000286
wdenk232c1502004-03-12 00:14:09 +0000287 while ((ext < end) && (*ext != 0xff)) {
288 if (*ext == 0) {
289 ext++;
290 } else {
291 u8 *opt = ext;
292
293 ext += ext[1] + 2;
294 if (ext <= end)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000295 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000296 }
wdenk3861aa52002-09-27 23:19:37 +0000297 }
wdenk3861aa52002-09-27 23:19:37 +0000298
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000299 debug("[BOOTP] Received fields:\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500300 if (net_netmask.s_addr)
301 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000302
Joe Hershberger049a95a2015-04-08 01:41:01 -0500303 if (net_gateway.s_addr)
304 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000305
Robin Getz0ebf04c2009-07-23 03:01:03 -0400306 if (NetBootFileSize)
307 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000308
Robin Getz0ebf04c2009-07-23 03:01:03 -0400309 if (NetOurHostName[0])
310 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000311
Robin Getz0ebf04c2009-07-23 03:01:03 -0400312 if (NetOurRootPath[0])
313 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000314
Robin Getz0ebf04c2009-07-23 03:01:03 -0400315 if (NetOurNISDomain[0])
316 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000317
Robin Getz0ebf04c2009-07-23 03:01:03 -0400318 if (NetBootFileSize)
319 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000320
321#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500322 if (net_ntp_server)
323 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000324#endif
wdenk3861aa52002-09-27 23:19:37 +0000325}
Simon Glass09349862011-06-13 16:13:12 -0700326
wdenk3861aa52002-09-27 23:19:37 +0000327/*
328 * Handle a BOOTP received packet.
329 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500330static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
331 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000332{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000333 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000334
Robin Getz0ebf04c2009-07-23 03:01:03 -0400335 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000336 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000337
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000338 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000339
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000340 /* Filter out pkts we don't want */
341 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000342 return;
343
344 /*
wdenk232c1502004-03-12 00:14:09 +0000345 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000346 */
347#ifdef CONFIG_STATUS_LED
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000348 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000349#endif
350
351 BootpCopyNetParams(bp); /* Store net parameters from reply */
352
353 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000354 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200355 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000356
357 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000358 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000359
Robin Getz0ebf04c2009-07-23 03:01:03 -0400360 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000361
Simon Glasse4a3d572011-10-27 06:24:32 +0000362 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000363}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500364#endif
wdenk3861aa52002-09-27 23:19:37 +0000365
366/*
367 * Timeout on BOOTP/DHCP request.
368 */
369static void
370BootpTimeout(void)
371{
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600372 ulong time_taken = get_timer(bootp_start);
373
374 if (time_taken >= TIMEOUT_MS) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000375#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600376 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000377 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000378#else
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600379 puts("\nRetry time exceeded; starting again\n");
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000380 NetStartAgain();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000381#endif
wdenk3861aa52002-09-27 23:19:37 +0000382 } else {
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600383 bootp_timeout *= 2;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200384 if (bootp_timeout > 2000)
385 bootp_timeout = 2000;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600386 NetSetTimeout(bootp_timeout, BootpTimeout);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000387 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000388 }
389}
390
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000391#define put_vci(e, str) \
392 do { \
393 size_t vci_strlen = strlen(str); \
394 *e++ = 60; /* Vendor Class Identifier */ \
395 *e++ = vci_strlen; \
396 memcpy(e, str, vci_strlen); \
397 e += vci_strlen; \
398 } while (0)
399
wdenk3861aa52002-09-27 23:19:37 +0000400/*
401 * Initialize BOOTP extension fields in the request.
402 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500403#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500404static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
405 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000406{
wdenk232c1502004-03-12 00:14:09 +0000407 u8 *start = e;
408 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000409#if defined(CONFIG_BOOTP_PXE)
410 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000411 u16 clientarch;
412#endif
wdenk232c1502004-03-12 00:14:09 +0000413
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500414#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000415 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000416#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500417#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200418 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000419#endif
wdenk3861aa52002-09-27 23:19:37 +0000420
wdenk232c1502004-03-12 00:14:09 +0000421 *e++ = 99; /* RFC1048 Magic Cookie */
422 *e++ = 130;
423 *e++ = 83;
424 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000425
wdenk232c1502004-03-12 00:14:09 +0000426 *e++ = 53; /* DHCP Message Type */
427 *e++ = 1;
428 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000429
wdenk232c1502004-03-12 00:14:09 +0000430 *e++ = 57; /* Maximum DHCP Message Size */
431 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000432 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
433 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000434
Joe Hershberger049a95a2015-04-08 01:41:01 -0500435 if (server_ip.s_addr) {
436 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000437
wdenk232c1502004-03-12 00:14:09 +0000438 *e++ = 54; /* ServerID */
439 *e++ = 4;
440 *e++ = tmp >> 24;
441 *e++ = tmp >> 16;
442 *e++ = tmp >> 8;
443 *e++ = tmp & 0xff;
444 }
wdenk3861aa52002-09-27 23:19:37 +0000445
Joe Hershberger049a95a2015-04-08 01:41:01 -0500446 if (requested_ip.s_addr) {
447 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000448
wdenk232c1502004-03-12 00:14:09 +0000449 *e++ = 50; /* Requested IP */
450 *e++ = 4;
451 *e++ = tmp >> 24;
452 *e++ = tmp >> 16;
453 *e++ = tmp >> 8;
454 *e++ = tmp & 0xff;
455 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500456#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000457 hostname = getenv("hostname");
458 if (hostname) {
459 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000460
461 *e++ = 12; /* Hostname */
462 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000463 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000464 e += hostnamelen;
465 }
stroesefe389a82003-08-28 14:17:32 +0000466#endif
467
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000468#if defined(CONFIG_BOOTP_PXE)
469 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
470 *e++ = 93; /* Client System Architecture */
471 *e++ = 2;
472 *e++ = (clientarch >> 8) & 0xff;
473 *e++ = clientarch & 0xff;
474
475 *e++ = 94; /* Client Network Interface Identifier */
476 *e++ = 3;
477 *e++ = 1; /* type field for UNDI */
478 *e++ = 0; /* major revision */
479 *e++ = 0; /* minor revision */
480
481 uuid = getenv("pxeuuid");
482
483 if (uuid) {
484 if (uuid_str_valid(uuid)) {
485 *e++ = 97; /* Client Machine Identifier */
486 *e++ = 17;
487 *e++ = 0; /* type 0 - UUID */
488
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200489 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000490 e += 16;
491 } else {
492 printf("Invalid pxeuuid: %s\n", uuid);
493 }
494 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000495#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000496
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000497#ifdef CONFIG_BOOTP_VCI_STRING
498 put_vci(e, CONFIG_BOOTP_VCI_STRING);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000499#endif
500
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500501#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000502 x = dhcp_vendorex_prep(e);
503 if (x)
wdenk232c1502004-03-12 00:14:09 +0000504 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000505#endif
506
wdenk232c1502004-03-12 00:14:09 +0000507 *e++ = 55; /* Parameter Request List */
508 cnt = e++; /* Pointer to count of requested items */
509 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500510#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000511 *e++ = 1; /* Subnet Mask */
512 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000513#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500514#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000515 *e++ = 2;
516 *cnt += 1;
517#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500518#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000519 *e++ = 3; /* Router Option */
520 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000521#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500522#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000523 *e++ = 6; /* DNS Server(s) */
524 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000525#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500526#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000527 *e++ = 12; /* Hostname */
528 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000529#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500530#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000531 *e++ = 13; /* Boot File Size */
532 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000533#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500534#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000535 *e++ = 17; /* Boot path */
536 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000537#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500538#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000539 *e++ = 40; /* NIS Domain name request */
540 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000541#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500542#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000543 *e++ = 42;
544 *cnt += 1;
545#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800546 /* no options, so back up to avoid sending an empty request list */
547 if (*cnt == 0)
548 e -= 2;
549
wdenk232c1502004-03-12 00:14:09 +0000550 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000551
wdenk232c1502004-03-12 00:14:09 +0000552 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000553#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800554 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000555 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000556#endif
557
wdenk232c1502004-03-12 00:14:09 +0000558 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000559}
560
Jon Loeliger610f2e92007-07-10 11:05:02 -0500561#else
wdenk3861aa52002-09-27 23:19:37 +0000562/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000563 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000564 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500565static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000566{
wdenk232c1502004-03-12 00:14:09 +0000567 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000568
wdenk232c1502004-03-12 00:14:09 +0000569 *e++ = 99; /* RFC1048 Magic Cookie */
570 *e++ = 130;
571 *e++ = 83;
572 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000573
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500574#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000575 *e++ = 53; /* DHCP Message Type */
576 *e++ = 1;
577 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000578
wdenk232c1502004-03-12 00:14:09 +0000579 *e++ = 57; /* Maximum DHCP Message Size */
580 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000581 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
582 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500583#endif
wdenk3861aa52002-09-27 23:19:37 +0000584
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000585#if defined(CONFIG_BOOTP_VCI_STRING) || \
586 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
587#ifdef CONFIG_SPL_BUILD
588 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
589#else
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000590 put_vci(e, CONFIG_BOOTP_VCI_STRING);
591#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000592#endif
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000593
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500594#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000595 *e++ = 1; /* Subnet mask request */
596 *e++ = 4;
597 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000598#endif
599
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500600#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000601 *e++ = 3; /* Default gateway request */
602 *e++ = 4;
603 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000604#endif
605
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500606#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000607 *e++ = 6; /* Domain Name Server */
608 *e++ = 4;
609 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000610#endif
611
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500612#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000613 *e++ = 12; /* Host name request */
614 *e++ = 32;
615 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000616#endif
617
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500618#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000619 *e++ = 13; /* Boot file size */
620 *e++ = 2;
621 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000622#endif
623
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500624#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000625 *e++ = 17; /* Boot path */
626 *e++ = 32;
627 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000628#endif
629
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500630#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000631 *e++ = 40; /* NIS Domain name request */
632 *e++ = 32;
633 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000634#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000635#if defined(CONFIG_BOOTP_NTPSERVER)
636 *e++ = 42;
637 *e++ = 4;
638 e += 4;
639#endif
wdenk3861aa52002-09-27 23:19:37 +0000640
wdenk232c1502004-03-12 00:14:09 +0000641 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000642
wdenk232c1502004-03-12 00:14:09 +0000643 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000644}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500645#endif
wdenk3861aa52002-09-27 23:19:37 +0000646
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600647void BootpReset(void)
648{
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200649 bootp_num_ids = 0;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600650 BootpTry = 0;
651 bootp_start = get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200652 bootp_timeout = 250;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600653}
654
wdenk3861aa52002-09-27 23:19:37 +0000655void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000656BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000657{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000658 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000659 struct Bootp_t *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000660 int extlen, pktlen, iplen;
661 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000662#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200663 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000664#endif
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200665 ulong BootpID;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500666 struct in_addr zero_ip;
667 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000668
Simon Glass573f14f2011-12-10 11:08:06 +0000669 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500670#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000671 dhcp_state = INIT;
672#endif
673
674#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000675 if (BootpTry == 0)
676 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000677
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000678 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
679 rand_ms = rand() >> (22 - BootpTry);
680 else /* After 3rd BOOTP request max 8192 * 1ms */
681 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000682
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000683 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200684 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000685
wdenk3861aa52002-09-27 23:19:37 +0000686#endif /* CONFIG_BOOTP_RANDOM_DELAY */
687
688 printf("BOOTP broadcast %d\n", ++BootpTry);
689 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000690 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000691
Joe Hershbergerae446f52012-05-23 07:59:10 +0000692 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
693 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000694
695 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000696 * Next line results in incorrect packet size being transmitted,
697 * resulting in errors in some DHCP servers, reporting missing bytes.
698 * Size must be set in packet header after extension length has been
699 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000700 * C. Hallinan, DS4.COM, Inc.
701 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000702 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000703 sizeof (struct Bootp_t)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000704 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000705 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000706
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000707 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000708 bp->bp_op = OP_BOOTREQUEST;
709 bp->bp_htype = HWT_ETHER;
710 bp->bp_hlen = HWL_ETHER;
711 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200712 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500713 zero_ip.s_addr = 0;
714 net_write_ip(&bp->bp_ciaddr, zero_ip);
715 net_write_ip(&bp->bp_yiaddr, zero_ip);
716 net_write_ip(&bp->bp_siaddr, zero_ip);
717 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000718 memcpy(bp->bp_chaddr, NetOurEther, 6);
719 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000720
721 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500722#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500723 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
724 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000725#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500726 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500727#endif
wdenk3861aa52002-09-27 23:19:37 +0000728
729 /*
730 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200731 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000732 */
733 BootpID = ((ulong)NetOurEther[2] << 24)
734 | ((ulong)NetOurEther[3] << 16)
735 | ((ulong)NetOurEther[4] << 8)
736 | (ulong)NetOurEther[5];
737 BootpID += get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200738 BootpID = htonl(BootpID);
739 bootp_add_id(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000740 NetCopyLong(&bp->bp_id, &BootpID);
741
742 /*
743 * Calculate proper packet lengths taking into account the
744 * variable size of the options field
745 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000746 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
747 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500748 bcast_ip.s_addr = 0xFFFFFFFFL;
749 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600750 NetSetTimeout(bootp_timeout, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000751
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500752#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000753 dhcp_state = SELECTING;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500754 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000755#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500756 net_set_udp_handler(bootp_handler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500757#endif
wdenk3861aa52002-09-27 23:19:37 +0000758 NetSendPacket(NetTxPacket, pktlen);
759}
760
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500761#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000762static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000763{
wdenk3e386912003-04-05 00:53:31 +0000764 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000765 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200766#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
767 int *to_ptr;
768#endif
wdenk3861aa52002-09-27 23:19:37 +0000769
wdenk232c1502004-03-12 00:14:09 +0000770 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000771 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000772 switch (*popt) {
773 case 1:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500774 net_copy_ip(&net_netmask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000775 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500776#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000777 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200778 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000779 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
780 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000781 break;
782#endif
wdenk232c1502004-03-12 00:14:09 +0000783 case 3:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500784 net_copy_ip(&net_gateway, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000785 break;
786 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500787 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500788#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000789 if (*(popt + 1) > 4)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500790 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000791#endif
wdenk232c1502004-03-12 00:14:09 +0000792 break;
793 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000794 size = truncate_sz("Host Name",
795 sizeof(NetOurHostName), oplen);
796 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000797 NetOurHostName[size] = 0;
798 break;
799 case 15: /* Ignore Domain Name Option */
800 break;
801 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000802 size = truncate_sz("Root Path",
803 sizeof(NetOurRootPath), oplen);
804 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000805 NetOurRootPath[size] = 0;
806 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000807 case 28: /* Ignore Broadcast Address Option */
808 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500809#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000810 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500811 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000812 break;
813#endif
wdenk232c1502004-03-12 00:14:09 +0000814 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000815 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000816 break;
817 case 53: /* Ignore Message Type Option */
818 break;
819 case 54:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500820 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000821 break;
822 case 58: /* Ignore Renewal Time Option */
823 break;
824 case 59: /* Ignore Rebinding Time Option */
825 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100826 case 66: /* Ignore TFTP server name */
827 break;
828 case 67: /* vendor opt bootfile */
829 /*
830 * I can't use dhcp_vendorex_proc here because I need
831 * to write into the bootp packet - even then I had to
832 * pass the bootp packet pointer into here as the
833 * second arg
834 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000835 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100836 sizeof(bp->bp_file),
837 oplen);
838 if (bp->bp_file[0] == '\0' && size > 0) {
839 /*
840 * only use vendor boot file if we didn't
841 * receive a boot file in the main non-vendor
842 * part of the packet - god only knows why
843 * some vendors chose not to use this perfectly
844 * good spot to store the boot file (join on
845 * Tru64 Unix) it seems mind bogglingly crazy
846 * to me
847 */
848 printf("*** WARNING: using vendor "
849 "optional boot file\n");
850 memcpy(bp->bp_file, popt + 2, size);
851 bp->bp_file[size] = '\0';
852 }
853 break;
wdenk232c1502004-03-12 00:14:09 +0000854 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500855#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000856 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000857 break;
wdenk3861aa52002-09-27 23:19:37 +0000858#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000859 printf("*** Unhandled DHCP Option in OFFER/ACK:"
860 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000861 break;
wdenk3861aa52002-09-27 23:19:37 +0000862 }
863 popt += oplen + 2; /* Process next option */
864 }
865}
866
867static int DhcpMessageType(unsigned char *popt)
868{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000869 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000870 return -1;
871
872 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000873 while (*popt != 0xff) {
874 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000875 return *(popt + 2);
876 popt += *(popt + 1) + 2; /* Scan through all options */
877 }
878 return -1;
879}
880
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000881static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000882{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000883 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000884 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000885 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000886 int eth_hdr_size;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500887 struct in_addr offered_ip;
888 struct in_addr zero_ip;
889 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000890
Robin Getz0ebf04c2009-07-23 03:01:03 -0400891 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000892 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000893 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000894
Joe Hershbergerae446f52012-05-23 07:59:10 +0000895 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
896 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000897
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000898 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000899 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000900
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000901 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000902 bp->bp_op = OP_BOOTREQUEST;
903 bp->bp_htype = HWT_ETHER;
904 bp->bp_hlen = HWL_ETHER;
905 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200906 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000907 /* Do not set the client IP, your IP, or server IP yet, since it
908 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400909
Wolfgang Denkc6686702006-10-12 00:01:08 +0200910 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200911 * RFC3046 requires Relay Agents to discard packets with
912 * nonzero and offered giaddr
913 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500914 zero_ip.s_addr = 0;
915 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200916
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000917 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000918
919 /*
920 * ID is the id of the OFFER packet
921 */
922
923 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
924
925 /*
926 * Copy options from OFFER packet if present
927 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400928
929 /* Copy offered IP into the parameters request list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500930 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
931 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
932 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +0000933
Joe Hershbergerae446f52012-05-23 07:59:10 +0000934 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
935 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500936 bcast_ip.s_addr = 0xFFFFFFFFL;
937 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000938
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100939#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
940 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
941#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000942 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000943 NetSendPacket(NetTxPacket, pktlen);
944}
945
946/*
947 * Handle DHCP received packets.
948 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500949static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
950 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000951{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000952 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000953
Robin Getz0ebf04c2009-07-23 03:01:03 -0400954 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000955 src, dest, len, dhcp_state);
956
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000957 /* Filter out pkts we don't want */
958 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000959 return;
960
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000961 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
962 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000963
964 switch (dhcp_state) {
965 case SELECTING:
966 /*
967 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000968 * to arrive. Then select one, and generate DHCPREQUEST
969 * response. If filename is in format we recognize, assume it
970 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000971 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400972 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200973#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000974 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200975 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000976 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200977#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000978
Robin Getz0ebf04c2009-07-23 03:01:03 -0400979 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000980 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000981
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000982 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
983 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100984 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000985
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600986 NetSetTimeout(5000, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000987 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200988#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000989 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200990#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000991
992 return;
993 break;
994 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400995 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000996
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000997 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
998 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
999 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +01001000 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001001 /* Store net params from reply */
1002 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +00001003 dhcp_state = BOUND;
Thierry Reding92ac8ac2014-08-19 10:21:24 +02001004 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger049a95a2015-04-08 01:41:01 -05001005 &net_ip, get_timer(bootp_start));
Simon Glass573f14f2011-12-10 11:08:06 +00001006 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001007 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001008
Simon Glasse4a3d572011-10-27 06:24:32 +00001009 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001010 return;
1011 }
1012 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +02001013 case BOUND:
1014 /* DHCP client bound to address */
1015 break;
wdenk3861aa52002-09-27 23:19:37 +00001016 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001017 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001018 break;
1019 }
1020
1021}
1022
1023void DhcpRequest(void)
1024{
1025 BootpRequest();
1026}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001027#endif /* CONFIG_CMD_DHCP */