blob: d4c86cf1794e7d78feafc837195f41a596a7dd6d [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;
63static IPaddr_t NetDHCPServerIP;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000064static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
65 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 Hershberger1752f0f2012-05-23 07:59:18 +0000142 IPaddr_t tmp_ip;
143
wdenk3d3befa2004-03-14 15:06:13 +0000144 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
145 if (tmp_ip != 0)
146 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershbergercb487f52012-05-23 07:58:06 +0000147 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400148#endif
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000149 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
wdenk3d3befa2004-03-14 15:06:13 +0000150 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000151 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000152
Robin Getz0ebf04c2009-07-23 03:01:03 -0400153 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000154
155 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000156 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000157 * not contain a new value
158 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000159 if (*BootFile)
160 setenv("bootfile", BootFile);
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 */
wdenk3861aa52002-09-27 23:19:37 +0000187 if (NetOurSubnetMask == 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000188 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (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 */
194 if (NetOurGatewayIP == 0)
195 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (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 Hershberger3090b7e32012-05-15 08:59:06 +0000202 if (NetOurDNSIP == 0)
203 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500204#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000205 if ((NetOurDNS2IP == 0) && (size > 4))
206 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000207#endif
wdenk3861aa52002-09-27 23:19:37 +0000208 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000209 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000210 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000211 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000212 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000213 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000214 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000215 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000216 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000217 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000218 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000219 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000220 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000221 size = truncate_sz("Host Name",
222 sizeof(NetOurHostName), size);
223 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000224 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000225 }
226 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000227 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000228 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000229 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000230 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000231 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000232 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000233 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000234 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000235 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000236 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000237 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000238 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000239 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000240 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000241 size = truncate_sz("Root Path",
242 sizeof(NetOurRootPath), size);
243 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000244 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000245 }
246 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000247 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000248 /*
wdenk8bde7f72003-06-27 21:31:46 +0000249 * This can be used to send the information of the
250 * vendor area in another file that the client can
251 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000252 */
253 break;
wdenk232c1502004-03-12 00:14:09 +0000254 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000255 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000256 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000257 size = truncate_sz("NIS Domain Name",
258 sizeof(NetOurNISDomain), size);
259 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000260 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000261 }
262 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000263#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
264 case 42: /* NTP server IP */
265 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
266 break;
267#endif
wdenk232c1502004-03-12 00:14:09 +0000268 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000269 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000270 /*
wdenk8bde7f72003-06-27 21:31:46 +0000271 * Binary information to exchange specific
272 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000273 */
274 break;
wdenk232c1502004-03-12 00:14:09 +0000275 /* Reserved (custom) fields (128..254) */
276 }
wdenk3861aa52002-09-27 23:19:37 +0000277}
278
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000279static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000280{
wdenk232c1502004-03-12 00:14:09 +0000281 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000282
Robin Getz0ebf04c2009-07-23 03:01:03 -0400283 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000284
wdenk232c1502004-03-12 00:14:09 +0000285 while ((ext < end) && (*ext != 0xff)) {
286 if (*ext == 0) {
287 ext++;
288 } else {
289 u8 *opt = ext;
290
291 ext += ext[1] + 2;
292 if (ext <= end)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000293 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000294 }
wdenk3861aa52002-09-27 23:19:37 +0000295 }
wdenk3861aa52002-09-27 23:19:37 +0000296
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000297 debug("[BOOTP] Received fields:\n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500298 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400299 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000300
Mike Frysingerb6446b62009-02-17 00:00:53 -0500301 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400302 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000303
Robin Getz0ebf04c2009-07-23 03:01:03 -0400304 if (NetBootFileSize)
305 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000306
Robin Getz0ebf04c2009-07-23 03:01:03 -0400307 if (NetOurHostName[0])
308 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000309
Robin Getz0ebf04c2009-07-23 03:01:03 -0400310 if (NetOurRootPath[0])
311 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000312
Robin Getz0ebf04c2009-07-23 03:01:03 -0400313 if (NetOurNISDomain[0])
314 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000315
Robin Getz0ebf04c2009-07-23 03:01:03 -0400316 if (NetBootFileSize)
317 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000318
319#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
320 if (NetNtpServerIP)
321 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
322#endif
wdenk3861aa52002-09-27 23:19:37 +0000323}
Simon Glass09349862011-06-13 16:13:12 -0700324
wdenk3861aa52002-09-27 23:19:37 +0000325/*
326 * Handle a BOOTP received packet.
327 */
328static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000329BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
330 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000331{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000332 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000333
Robin Getz0ebf04c2009-07-23 03:01:03 -0400334 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000335 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000336
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000337 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000338
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000339 /* Filter out pkts we don't want */
340 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000341 return;
342
343 /*
wdenk232c1502004-03-12 00:14:09 +0000344 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000345 */
346#ifdef CONFIG_STATUS_LED
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000347 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000348#endif
349
350 BootpCopyNetParams(bp); /* Store net parameters from reply */
351
352 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000353 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200354 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000355
356 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000357 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000358
Robin Getz0ebf04c2009-07-23 03:01:03 -0400359 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000360
Simon Glasse4a3d572011-10-27 06:24:32 +0000361 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000362}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500363#endif
wdenk3861aa52002-09-27 23:19:37 +0000364
365/*
366 * Timeout on BOOTP/DHCP request.
367 */
368static void
369BootpTimeout(void)
370{
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600371 ulong time_taken = get_timer(bootp_start);
372
373 if (time_taken >= TIMEOUT_MS) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000374#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600375 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000376 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000377#else
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600378 puts("\nRetry time exceeded; starting again\n");
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000379 NetStartAgain();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000380#endif
wdenk3861aa52002-09-27 23:19:37 +0000381 } else {
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600382 bootp_timeout *= 2;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200383 if (bootp_timeout > 2000)
384 bootp_timeout = 2000;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600385 NetSetTimeout(bootp_timeout, BootpTimeout);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000386 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000387 }
388}
389
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000390#define put_vci(e, str) \
391 do { \
392 size_t vci_strlen = strlen(str); \
393 *e++ = 60; /* Vendor Class Identifier */ \
394 *e++ = vci_strlen; \
395 memcpy(e, str, vci_strlen); \
396 e += vci_strlen; \
397 } while (0)
398
wdenk3861aa52002-09-27 23:19:37 +0000399/*
400 * Initialize BOOTP extension fields in the request.
401 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500402#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000403static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
404 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000405{
wdenk232c1502004-03-12 00:14:09 +0000406 u8 *start = e;
407 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000408#if defined(CONFIG_BOOTP_PXE)
409 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000410 u16 clientarch;
411#endif
wdenk232c1502004-03-12 00:14:09 +0000412
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500413#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000414 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000415#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500416#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200417 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000418#endif
wdenk3861aa52002-09-27 23:19:37 +0000419
wdenk232c1502004-03-12 00:14:09 +0000420 *e++ = 99; /* RFC1048 Magic Cookie */
421 *e++ = 130;
422 *e++ = 83;
423 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000424
wdenk232c1502004-03-12 00:14:09 +0000425 *e++ = 53; /* DHCP Message Type */
426 *e++ = 1;
427 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000428
wdenk232c1502004-03-12 00:14:09 +0000429 *e++ = 57; /* Maximum DHCP Message Size */
430 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000431 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
432 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000433
wdenk232c1502004-03-12 00:14:09 +0000434 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000435 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000436
wdenk232c1502004-03-12 00:14:09 +0000437 *e++ = 54; /* ServerID */
438 *e++ = 4;
439 *e++ = tmp >> 24;
440 *e++ = tmp >> 16;
441 *e++ = tmp >> 8;
442 *e++ = tmp & 0xff;
443 }
wdenk3861aa52002-09-27 23:19:37 +0000444
wdenk232c1502004-03-12 00:14:09 +0000445 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000446 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000447
wdenk232c1502004-03-12 00:14:09 +0000448 *e++ = 50; /* Requested IP */
449 *e++ = 4;
450 *e++ = tmp >> 24;
451 *e++ = tmp >> 16;
452 *e++ = tmp >> 8;
453 *e++ = tmp & 0xff;
454 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500455#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000456 hostname = getenv("hostname");
457 if (hostname) {
458 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000459
460 *e++ = 12; /* Hostname */
461 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000462 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000463 e += hostnamelen;
464 }
stroesefe389a82003-08-28 14:17:32 +0000465#endif
466
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000467#if defined(CONFIG_BOOTP_PXE)
468 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
469 *e++ = 93; /* Client System Architecture */
470 *e++ = 2;
471 *e++ = (clientarch >> 8) & 0xff;
472 *e++ = clientarch & 0xff;
473
474 *e++ = 94; /* Client Network Interface Identifier */
475 *e++ = 3;
476 *e++ = 1; /* type field for UNDI */
477 *e++ = 0; /* major revision */
478 *e++ = 0; /* minor revision */
479
480 uuid = getenv("pxeuuid");
481
482 if (uuid) {
483 if (uuid_str_valid(uuid)) {
484 *e++ = 97; /* Client Machine Identifier */
485 *e++ = 17;
486 *e++ = 0; /* type 0 - UUID */
487
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200488 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000489 e += 16;
490 } else {
491 printf("Invalid pxeuuid: %s\n", uuid);
492 }
493 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000494#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000495
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000496#ifdef CONFIG_BOOTP_VCI_STRING
497 put_vci(e, CONFIG_BOOTP_VCI_STRING);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000498#endif
499
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500500#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000501 x = dhcp_vendorex_prep(e);
502 if (x)
wdenk232c1502004-03-12 00:14:09 +0000503 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000504#endif
505
wdenk232c1502004-03-12 00:14:09 +0000506 *e++ = 55; /* Parameter Request List */
507 cnt = e++; /* Pointer to count of requested items */
508 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500509#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000510 *e++ = 1; /* Subnet Mask */
511 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000512#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500513#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000514 *e++ = 2;
515 *cnt += 1;
516#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500517#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000518 *e++ = 3; /* Router Option */
519 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000520#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500521#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000522 *e++ = 6; /* DNS Server(s) */
523 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000524#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500525#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000526 *e++ = 12; /* Hostname */
527 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000528#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500529#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000530 *e++ = 13; /* Boot File Size */
531 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000532#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500533#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000534 *e++ = 17; /* Boot path */
535 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500537#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000538 *e++ = 40; /* NIS Domain name request */
539 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000540#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500541#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000542 *e++ = 42;
543 *cnt += 1;
544#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800545 /* no options, so back up to avoid sending an empty request list */
546 if (*cnt == 0)
547 e -= 2;
548
wdenk232c1502004-03-12 00:14:09 +0000549 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000550
wdenk232c1502004-03-12 00:14:09 +0000551 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000552#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800553 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000554 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000555#endif
556
wdenk232c1502004-03-12 00:14:09 +0000557 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000558}
559
Jon Loeliger610f2e92007-07-10 11:05:02 -0500560#else
wdenk3861aa52002-09-27 23:19:37 +0000561/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000562 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000563 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000564static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000565{
wdenk232c1502004-03-12 00:14:09 +0000566 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000567
wdenk232c1502004-03-12 00:14:09 +0000568 *e++ = 99; /* RFC1048 Magic Cookie */
569 *e++ = 130;
570 *e++ = 83;
571 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000572
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500573#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000574 *e++ = 53; /* DHCP Message Type */
575 *e++ = 1;
576 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000577
wdenk232c1502004-03-12 00:14:09 +0000578 *e++ = 57; /* Maximum DHCP Message Size */
579 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000580 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
581 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500582#endif
wdenk3861aa52002-09-27 23:19:37 +0000583
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000584#if defined(CONFIG_BOOTP_VCI_STRING) || \
585 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
586#ifdef CONFIG_SPL_BUILD
587 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
588#else
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000589 put_vci(e, CONFIG_BOOTP_VCI_STRING);
590#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000591#endif
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000592
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500593#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000594 *e++ = 1; /* Subnet mask request */
595 *e++ = 4;
596 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000597#endif
598
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500599#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000600 *e++ = 3; /* Default gateway request */
601 *e++ = 4;
602 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000603#endif
604
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500605#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000606 *e++ = 6; /* Domain Name Server */
607 *e++ = 4;
608 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000609#endif
610
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500611#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000612 *e++ = 12; /* Host name request */
613 *e++ = 32;
614 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000615#endif
616
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500617#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000618 *e++ = 13; /* Boot file size */
619 *e++ = 2;
620 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000621#endif
622
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500623#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000624 *e++ = 17; /* Boot path */
625 *e++ = 32;
626 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000627#endif
628
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500629#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000630 *e++ = 40; /* NIS Domain name request */
631 *e++ = 32;
632 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000633#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000634#if defined(CONFIG_BOOTP_NTPSERVER)
635 *e++ = 42;
636 *e++ = 4;
637 e += 4;
638#endif
wdenk3861aa52002-09-27 23:19:37 +0000639
wdenk232c1502004-03-12 00:14:09 +0000640 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000641
wdenk232c1502004-03-12 00:14:09 +0000642 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000643}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500644#endif
wdenk3861aa52002-09-27 23:19:37 +0000645
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600646void BootpReset(void)
647{
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200648 bootp_num_ids = 0;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600649 BootpTry = 0;
650 bootp_start = get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200651 bootp_timeout = 250;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600652}
653
wdenk3861aa52002-09-27 23:19:37 +0000654void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000655BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000656{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000657 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000658 struct Bootp_t *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000659 int extlen, pktlen, iplen;
660 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000661#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200662 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000663#endif
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200664 ulong BootpID;
wdenk3861aa52002-09-27 23:19:37 +0000665
Simon Glass573f14f2011-12-10 11:08:06 +0000666 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500667#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000668 dhcp_state = INIT;
669#endif
670
671#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000672 if (BootpTry == 0)
673 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000674
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000675 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
676 rand_ms = rand() >> (22 - BootpTry);
677 else /* After 3rd BOOTP request max 8192 * 1ms */
678 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000679
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000680 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200681 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000682
wdenk3861aa52002-09-27 23:19:37 +0000683#endif /* CONFIG_BOOTP_RANDOM_DELAY */
684
685 printf("BOOTP broadcast %d\n", ++BootpTry);
686 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000687 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000688
Joe Hershbergerae446f52012-05-23 07:59:10 +0000689 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
690 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000691
692 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000693 * Next line results in incorrect packet size being transmitted,
694 * resulting in errors in some DHCP servers, reporting missing bytes.
695 * Size must be set in packet header after extension length has been
696 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000697 * C. Hallinan, DS4.COM, Inc.
698 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000699 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000700 sizeof (struct Bootp_t)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000701 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000702 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000703
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000704 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000705 bp->bp_op = OP_BOOTREQUEST;
706 bp->bp_htype = HWT_ETHER;
707 bp->bp_hlen = HWL_ETHER;
708 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200709 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000710 NetWriteIP(&bp->bp_ciaddr, 0);
711 NetWriteIP(&bp->bp_yiaddr, 0);
712 NetWriteIP(&bp->bp_siaddr, 0);
713 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000714 memcpy(bp->bp_chaddr, NetOurEther, 6);
715 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000716
717 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500718#if defined(CONFIG_CMD_DHCP)
Joe Hershbergerae446f52012-05-23 07:59:10 +0000719 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000720#else
Joe Hershbergerae446f52012-05-23 07:59:10 +0000721 extlen = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500722#endif
wdenk3861aa52002-09-27 23:19:37 +0000723
724 /*
725 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200726 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000727 */
728 BootpID = ((ulong)NetOurEther[2] << 24)
729 | ((ulong)NetOurEther[3] << 16)
730 | ((ulong)NetOurEther[4] << 8)
731 | (ulong)NetOurEther[5];
732 BootpID += get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200733 BootpID = htonl(BootpID);
734 bootp_add_id(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000735 NetCopyLong(&bp->bp_id, &BootpID);
736
737 /*
738 * Calculate proper packet lengths taking into account the
739 * variable size of the options field
740 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000741 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
742 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000743 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600744 NetSetTimeout(bootp_timeout, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000745
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500746#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000747 dhcp_state = SELECTING;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000748 net_set_udp_handler(DhcpHandler);
wdenk3861aa52002-09-27 23:19:37 +0000749#else
Joe Hershbergerece223b2012-05-23 07:59:15 +0000750 net_set_udp_handler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500751#endif
wdenk3861aa52002-09-27 23:19:37 +0000752 NetSendPacket(NetTxPacket, pktlen);
753}
754
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500755#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000756static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000757{
wdenk3e386912003-04-05 00:53:31 +0000758 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000759 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200760#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
761 int *to_ptr;
762#endif
wdenk3861aa52002-09-27 23:19:37 +0000763
wdenk232c1502004-03-12 00:14:09 +0000764 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000765 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000766 switch (*popt) {
767 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000768 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000769 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500770#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000771 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200772 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000773 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
774 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000775 break;
776#endif
wdenk232c1502004-03-12 00:14:09 +0000777 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000778 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000779 break;
780 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000781 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500782#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000783 if (*(popt + 1) > 4)
784 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000785#endif
wdenk232c1502004-03-12 00:14:09 +0000786 break;
787 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000788 size = truncate_sz("Host Name",
789 sizeof(NetOurHostName), oplen);
790 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000791 NetOurHostName[size] = 0;
792 break;
793 case 15: /* Ignore Domain Name Option */
794 break;
795 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000796 size = truncate_sz("Root Path",
797 sizeof(NetOurRootPath), oplen);
798 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000799 NetOurRootPath[size] = 0;
800 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000801 case 28: /* Ignore Broadcast Address Option */
802 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500803#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000804 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000805 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000806 break;
807#endif
wdenk232c1502004-03-12 00:14:09 +0000808 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000809 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000810 break;
811 case 53: /* Ignore Message Type Option */
812 break;
813 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000814 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000815 break;
816 case 58: /* Ignore Renewal Time Option */
817 break;
818 case 59: /* Ignore Rebinding Time Option */
819 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100820 case 66: /* Ignore TFTP server name */
821 break;
822 case 67: /* vendor opt bootfile */
823 /*
824 * I can't use dhcp_vendorex_proc here because I need
825 * to write into the bootp packet - even then I had to
826 * pass the bootp packet pointer into here as the
827 * second arg
828 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000829 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100830 sizeof(bp->bp_file),
831 oplen);
832 if (bp->bp_file[0] == '\0' && size > 0) {
833 /*
834 * only use vendor boot file if we didn't
835 * receive a boot file in the main non-vendor
836 * part of the packet - god only knows why
837 * some vendors chose not to use this perfectly
838 * good spot to store the boot file (join on
839 * Tru64 Unix) it seems mind bogglingly crazy
840 * to me
841 */
842 printf("*** WARNING: using vendor "
843 "optional boot file\n");
844 memcpy(bp->bp_file, popt + 2, size);
845 bp->bp_file[size] = '\0';
846 }
847 break;
wdenk232c1502004-03-12 00:14:09 +0000848 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500849#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000850 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000851 break;
wdenk3861aa52002-09-27 23:19:37 +0000852#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000853 printf("*** Unhandled DHCP Option in OFFER/ACK:"
854 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000855 break;
wdenk3861aa52002-09-27 23:19:37 +0000856 }
857 popt += oplen + 2; /* Process next option */
858 }
859}
860
861static int DhcpMessageType(unsigned char *popt)
862{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000863 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000864 return -1;
865
866 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000867 while (*popt != 0xff) {
868 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000869 return *(popt + 2);
870 popt += *(popt + 1) + 2; /* Scan through all options */
871 }
872 return -1;
873}
874
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000875static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000876{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000877 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000878 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000879 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000880 int eth_hdr_size;
wdenk47cd00f2003-03-06 13:39:27 +0000881 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000882
Robin Getz0ebf04c2009-07-23 03:01:03 -0400883 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000884 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000885 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000886
Joe Hershbergerae446f52012-05-23 07:59:10 +0000887 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
888 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000889
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000890 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000891 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000892
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000893 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000894 bp->bp_op = OP_BOOTREQUEST;
895 bp->bp_htype = HWT_ETHER;
896 bp->bp_hlen = HWL_ETHER;
897 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200898 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000899 /* Do not set the client IP, your IP, or server IP yet, since it
900 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400901
Wolfgang Denkc6686702006-10-12 00:01:08 +0200902 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200903 * RFC3046 requires Relay Agents to discard packets with
904 * nonzero and offered giaddr
905 */
906 NetWriteIP(&bp->bp_giaddr, 0);
907
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000908 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000909
910 /*
911 * ID is the id of the OFFER packet
912 */
913
914 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
915
916 /*
917 * Copy options from OFFER packet if present
918 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400919
920 /* Copy offered IP into the parameters request list */
921 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000922 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
923 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000924
Joe Hershbergerae446f52012-05-23 07:59:10 +0000925 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
926 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000927 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000928
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100929#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
930 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
931#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000932 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000933 NetSendPacket(NetTxPacket, pktlen);
934}
935
936/*
937 * Handle DHCP received packets.
938 */
939static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000940DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
941 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000942{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000943 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000944
Robin Getz0ebf04c2009-07-23 03:01:03 -0400945 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000946 src, dest, len, dhcp_state);
947
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000948 /* Filter out pkts we don't want */
949 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000950 return;
951
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000952 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
953 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000954
955 switch (dhcp_state) {
956 case SELECTING:
957 /*
958 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000959 * to arrive. Then select one, and generate DHCPREQUEST
960 * response. If filename is in format we recognize, assume it
961 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000962 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400963 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200964#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000965 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200966 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000967 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200968#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000969
Robin Getz0ebf04c2009-07-23 03:01:03 -0400970 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000971 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000972
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000973 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
974 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100975 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000976
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600977 NetSetTimeout(5000, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000978 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200979#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000980 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200981#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000982
983 return;
984 break;
985 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400986 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000987
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000988 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
989 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
990 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100991 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000992 /* Store net params from reply */
993 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000994 dhcp_state = BOUND;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200995 printf("DHCP client bound to address %pI4 (%lu ms)\n",
996 &NetOurIP, get_timer(bootp_start));
Simon Glass573f14f2011-12-10 11:08:06 +0000997 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000998 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000999
Simon Glasse4a3d572011-10-27 06:24:32 +00001000 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001001 return;
1002 }
1003 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +02001004 case BOUND:
1005 /* DHCP client bound to address */
1006 break;
wdenk3861aa52002-09-27 23:19:37 +00001007 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001008 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001009 break;
1010 }
1011
1012}
1013
1014void DhcpRequest(void)
1015{
1016 BootpRequest();
1017}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001018#endif /* CONFIG_CMD_DHCP */