]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/bootp.c
net: cosmetic: Fixup var names related to boot file
[people/ms/u-boot.git] / net / bootp.c
CommitLineData
3861aa5c
WD
1/*
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
232c150a 8 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
3861aa5c
WD
9 */
10
3861aa5c
WD
11#include <common.h>
12#include <command.h>
13#include <net.h>
14#include "bootp.h"
15#include "tftp.h"
232c150a 16#include "nfs.h"
3861aa5c
WD
17#ifdef CONFIG_STATUS_LED
18#include <status_led.h>
19#endif
db7720ba
KP
20#ifdef CONFIG_BOOTP_RANDOM_DELAY
21#include "net_rand.h"
22#endif
3861aa5c 23
3090b7e3 24#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
3861aa5c 25
f59be6e8
SW
26/*
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 */
232c150a 36#ifndef CONFIG_NET_RETRY_COUNT
3090b7e3 37# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
3861aa5c 38#else
232c150a 39# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
3861aa5c 40#endif
f59be6e8 41#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
3861aa5c 42
3090b7e3
JH
43#define PORT_BOOTPS 67 /* BOOTP server UDP port */
44#define PORT_BOOTPC 68 /* BOOTP client UDP port */
3861aa5c 45
3090b7e3 46#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
232c150a 47#define CONFIG_DHCP_MIN_EXT_LEN 64
3861aa5c
WD
48#endif
49
92ac8acc
TR
50#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;
3861aa5c 56int BootpTry;
f59be6e8
SW
57ulong bootp_start;
58ulong bootp_timeout;
3861aa5c 59
643d1ab2 60#if defined(CONFIG_CMD_DHCP)
06370590
KP
61static dhcp_state_t dhcp_state = INIT;
62static unsigned long dhcp_leasetime;
049a95a7
JH
63static struct in_addr dhcp_server_ip;
64static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
65 unsigned src, unsigned len);
3861aa5c
WD
66
67/* For Debug */
3e38691e
WD
68#if 0
69static char *dhcpmsg2str(int type)
3861aa5c
WD
70{
71 switch (type) {
232c150a
WD
72 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;
3861aa5c
WD
79 default: return "UNKNOWN/INVALID MSG TYPE"; break;
80 }
81}
3e38691e 82#endif
610f2e9c 83#endif
3861aa5c 84
92ac8acc
TR
85static 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
3861aa5c
WD
109static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
110{
3090b7e3 111 struct Bootp_t *bp = (struct Bootp_t *) pkt;
3861aa5c
WD
112 int retval = 0;
113
114 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
115 retval = -1;
f8315731 116 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
3861aa5c
WD
117 retval = -2;
118 else if (bp->bp_op != OP_BOOTREQUEST &&
3090b7e3
JH
119 bp->bp_op != OP_BOOTREPLY &&
120 bp->bp_op != DHCP_OFFER &&
121 bp->bp_op != DHCP_ACK &&
122 bp->bp_op != DHCP_NAK)
3861aa5c 123 retval = -3;
3861aa5c
WD
124 else if (bp->bp_htype != HWT_ETHER)
125 retval = -4;
126 else if (bp->bp_hlen != HWL_ETHER)
127 retval = -5;
92ac8acc 128 else if (!bootp_match_id(NetReadLong((ulong *)&bp->bp_id)))
3861aa5c 129 retval = -6;
3861aa5c 130
0ebf04c6 131 debug("Filtering pkt = %d\n", retval);
3861aa5c
WD
132
133 return retval;
134}
135
136/*
137 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
138 */
3090b7e3 139static void BootpCopyNetParams(struct Bootp_t *bp)
3861aa5c 140{
5d110f0a 141#if !defined(CONFIG_BOOTP_SERVERIP)
049a95a7 142 struct in_addr tmp_ip;
1752f0fd 143
049a95a7
JH
144 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);
cb487f56 147 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
3d3befa7 148 if (strlen(bp->bp_file) > 0)
1411157d
JH
149 copy_filename(net_boot_file_name, bp->bp_file,
150 sizeof(net_boot_file_name));
3861aa5c 151
1411157d 152 debug("net_boot_file_name: %s\n", net_boot_file_name);
3861aa5c
WD
153
154 /* Propagate to environment:
8bde7f77 155 * don't delete exising entry when BOOTP / DHCP reply does
3861aa5c
WD
156 * not contain a new value
157 */
1411157d
JH
158 if (*net_boot_file_name)
159 setenv("bootfile", net_boot_file_name);
ecec4e9c 160#endif
049a95a7 161 net_copy_ip(&net_ip, &bp->bp_yiaddr);
3861aa5c
WD
162}
163
3090b7e3 164static int truncate_sz(const char *name, int maxlen, int curlen)
3861aa5c
WD
165{
166 if (curlen >= maxlen) {
3090b7e3
JH
167 printf("*** WARNING: %s is too long (%d - max: %d)"
168 " - truncated\n", name, curlen, maxlen);
3861aa5c
WD
169 curlen = maxlen - 1;
170 }
3090b7e3 171 return curlen;
3861aa5c
WD
172}
173
643d1ab2 174#if !defined(CONFIG_CMD_DHCP)
3861aa5c 175
3090b7e3 176static void BootpVendorFieldProcess(u8 *ext)
3861aa5c 177{
232c150a 178 int size = *(ext + 1);
3861aa5c 179
0ebf04c6 180 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
3090b7e3 181 *(ext + 1));
3861aa5c 182
1411157d 183 net_boot_file_expected_size_in_blocks = 0;
3861aa5c 184
232c150a
WD
185 switch (*ext) {
186 /* Fixed length fields */
3090b7e3 187 case 1: /* Subnet mask */
049a95a7
JH
188 if (net_netmask.s_addr == 0)
189 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
3861aa5c 190 break;
3090b7e3 191 case 2: /* Time offset - Not yet supported */
3861aa5c 192 break;
232c150a 193 /* Variable length fields */
3090b7e3 194 case 3: /* Gateways list */
049a95a7
JH
195 if (net_gateway.s_addr == 0)
196 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
3861aa5c 197 break;
3090b7e3 198 case 4: /* Time server - Not yet supported */
3861aa5c 199 break;
3090b7e3 200 case 5: /* IEN-116 name server - Not yet supported */
3861aa5c
WD
201 break;
202 case 6:
049a95a7
JH
203 if (net_dns_server.s_addr == 0)
204 net_copy_ip(&net_dns_server,
205 (struct in_addr *)(ext + 2));
1fe80d79 206#if defined(CONFIG_BOOTP_DNS2)
049a95a7
JH
207 if ((net_dns_server2.s_addr == 0) && (size > 4))
208 net_copy_ip(&net_dns_server2,
209 (struct in_addr *)(ext + 2 + 4));
fe389a82 210#endif
3861aa5c 211 break;
3090b7e3 212 case 7: /* Log server - Not yet supported */
3861aa5c 213 break;
3090b7e3 214 case 8: /* Cookie/Quote server - Not yet supported */
3861aa5c 215 break;
3090b7e3 216 case 9: /* LPR server - Not yet supported */
3861aa5c 217 break;
3090b7e3 218 case 10: /* Impress server - Not yet supported */
3861aa5c 219 break;
3090b7e3 220 case 11: /* RPL server - Not yet supported */
3861aa5c 221 break;
3090b7e3 222 case 12: /* Host name */
3861aa5c 223 if (NetOurHostName[0] == 0) {
3090b7e3
JH
224 size = truncate_sz("Host Name",
225 sizeof(NetOurHostName), size);
226 memcpy(&NetOurHostName, ext + 2, size);
232c150a 227 NetOurHostName[size] = 0;
3861aa5c
WD
228 }
229 break;
3090b7e3 230 case 13: /* Boot file size */
3861aa5c 231 if (size == 2)
1411157d
JH
232 net_boot_file_expected_size_in_blocks =
233 ntohs(*(ushort *)(ext + 2));
3861aa5c 234 else if (size == 4)
1411157d
JH
235 net_boot_file_expected_size_in_blocks =
236 ntohl(*(ulong *)(ext + 2));
3861aa5c 237 break;
3090b7e3 238 case 14: /* Merit dump file - Not yet supported */
3861aa5c 239 break;
3090b7e3 240 case 15: /* Domain name - Not yet supported */
3861aa5c 241 break;
3090b7e3 242 case 16: /* Swap server - Not yet supported */
3861aa5c 243 break;
3090b7e3 244 case 17: /* Root path */
3861aa5c 245 if (NetOurRootPath[0] == 0) {
3090b7e3
JH
246 size = truncate_sz("Root Path",
247 sizeof(NetOurRootPath), size);
248 memcpy(&NetOurRootPath, ext + 2, size);
232c150a 249 NetOurRootPath[size] = 0;
3861aa5c
WD
250 }
251 break;
3090b7e3 252 case 18: /* Extension path - Not yet supported */
3861aa5c 253 /*
8bde7f77
WD
254 * This can be used to send the information of the
255 * vendor area in another file that the client can
256 * access via TFTP.
3861aa5c
WD
257 */
258 break;
232c150a 259 /* IP host layer fields */
3090b7e3 260 case 40: /* NIS Domain name */
3861aa5c 261 if (NetOurNISDomain[0] == 0) {
3090b7e3
JH
262 size = truncate_sz("NIS Domain Name",
263 sizeof(NetOurNISDomain), size);
264 memcpy(&NetOurNISDomain, ext + 2, size);
232c150a 265 NetOurNISDomain[size] = 0;
3861aa5c
WD
266 }
267 break;
09e3a67d
LP
268#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
269 case 42: /* NTP server IP */
049a95a7 270 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
09e3a67d
LP
271 break;
272#endif
232c150a 273 /* Application layer fields */
3090b7e3 274 case 43: /* Vendor specific info - Not yet supported */
3861aa5c 275 /*
8bde7f77
WD
276 * Binary information to exchange specific
277 * product information.
3861aa5c
WD
278 */
279 break;
232c150a
WD
280 /* Reserved (custom) fields (128..254) */
281 }
3861aa5c
WD
282}
283
3090b7e3 284static void BootpVendorProcess(u8 *ext, int size)
3861aa5c 285{
232c150a 286 u8 *end = ext + size;
3861aa5c 287
0ebf04c6 288 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
3861aa5c 289
232c150a
WD
290 while ((ext < end) && (*ext != 0xff)) {
291 if (*ext == 0) {
292 ext++;
293 } else {
294 u8 *opt = ext;
295
296 ext += ext[1] + 2;
297 if (ext <= end)
3090b7e3 298 BootpVendorFieldProcess(opt);
232c150a 299 }
3861aa5c 300 }
3861aa5c 301
3090b7e3 302 debug("[BOOTP] Received fields:\n");
049a95a7
JH
303 if (net_netmask.s_addr)
304 debug("net_netmask : %pI4\n", &net_netmask);
232c150a 305
049a95a7
JH
306 if (net_gateway.s_addr)
307 debug("net_gateway : %pI4", &net_gateway);
232c150a 308
1411157d
JH
309 if (net_boot_file_expected_size_in_blocks)
310 debug("net_boot_file_expected_size_in_blocks : %d\n",
311 net_boot_file_expected_size_in_blocks);
3861aa5c 312
0ebf04c6
RG
313 if (NetOurHostName[0])
314 debug("NetOurHostName : %s\n", NetOurHostName);
232c150a 315
0ebf04c6
RG
316 if (NetOurRootPath[0])
317 debug("NetOurRootPath : %s\n", NetOurRootPath);
232c150a 318
0ebf04c6
RG
319 if (NetOurNISDomain[0])
320 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
232c150a 321
09e3a67d 322#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
049a95a7
JH
323 if (net_ntp_server)
324 debug("net_ntp_server : %pI4\n", &net_ntp_server);
09e3a67d 325#endif
232c150a 326}
09349866 327
3861aa5c
WD
328/*
329 * Handle a BOOTP received packet.
330 */
049a95a7
JH
331static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
332 unsigned src, unsigned len)
3861aa5c 333{
3090b7e3 334 struct Bootp_t *bp;
3861aa5c 335
0ebf04c6 336 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
3090b7e3 337 src, dest, len, sizeof(struct Bootp_t));
3861aa5c 338
3090b7e3 339 bp = (struct Bootp_t *)pkt;
3861aa5c 340
3090b7e3
JH
341 /* Filter out pkts we don't want */
342 if (BootpCheckPkt(pkt, dest, src, len))
3861aa5c
WD
343 return;
344
345 /*
232c150a 346 * Got a good BOOTP reply. Copy the data into our variables.
3861aa5c
WD
347 */
348#ifdef CONFIG_STATUS_LED
3090b7e3 349 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
3861aa5c
WD
350#endif
351
352 BootpCopyNetParams(bp); /* Store net parameters from reply */
353
354 /* Retrieve extended information (we must parse the vendor area) */
3090b7e3 355 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
77ddac94 356 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
3861aa5c
WD
357
358 NetSetTimeout(0, (thand_f *)0);
573f14fe 359 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
3861aa5c 360
0ebf04c6 361 debug("Got good BOOTP\n");
3861aa5c 362
e4a3d57d 363 net_auto_load();
3861aa5c 364}
610f2e9c 365#endif
3861aa5c
WD
366
367/*
368 * Timeout on BOOTP/DHCP request.
369 */
370static void
371BootpTimeout(void)
372{
f59be6e8
SW
373 ulong time_taken = get_timer(bootp_start);
374
375 if (time_taken >= TIMEOUT_MS) {
2c00e099 376#ifdef CONFIG_BOOTP_MAY_FAIL
f59be6e8 377 puts("\nRetry time exceeded\n");
b977aa80 378 net_set_state(NETLOOP_FAIL);
2c00e099 379#else
f59be6e8 380 puts("\nRetry time exceeded; starting again\n");
3090b7e3 381 NetStartAgain();
2c00e099 382#endif
3861aa5c 383 } else {
f59be6e8 384 bootp_timeout *= 2;
92ac8acc
TR
385 if (bootp_timeout > 2000)
386 bootp_timeout = 2000;
f59be6e8 387 NetSetTimeout(bootp_timeout, BootpTimeout);
3090b7e3 388 BootpRequest();
3861aa5c
WD
389 }
390}
391
9ace17c8
IY
392#define put_vci(e, str) \
393 do { \
394 size_t vci_strlen = strlen(str); \
395 *e++ = 60; /* Vendor Class Identifier */ \
396 *e++ = vci_strlen; \
397 memcpy(e, str, vci_strlen); \
398 e += vci_strlen; \
399 } while (0)
400
3861aa5c
WD
401/*
402 * Initialize BOOTP extension fields in the request.
403 */
643d1ab2 404#if defined(CONFIG_CMD_DHCP)
049a95a7
JH
405static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
406 struct in_addr requested_ip)
3861aa5c 407{
232c150a
WD
408 u8 *start = e;
409 u8 *cnt;
d2b5d5c4
JH
410#if defined(CONFIG_BOOTP_PXE)
411 char *uuid;
d2b5d5c4
JH
412 u16 clientarch;
413#endif
232c150a 414
1fe80d79 415#if defined(CONFIG_BOOTP_VENDOREX)
232c150a 416 u8 *x;
3861aa5c 417#endif
1fe80d79 418#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
77ddac94 419 char *hostname;
fe389a82 420#endif
3861aa5c 421
232c150a
WD
422 *e++ = 99; /* RFC1048 Magic Cookie */
423 *e++ = 130;
424 *e++ = 83;
425 *e++ = 99;
3861aa5c 426
232c150a
WD
427 *e++ = 53; /* DHCP Message Type */
428 *e++ = 1;
429 *e++ = message_type;
3861aa5c 430
232c150a
WD
431 *e++ = 57; /* Maximum DHCP Message Size */
432 *e++ = 2;
f8315731
JH
433 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
434 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
3861aa5c 435
049a95a7
JH
436 if (server_ip.s_addr) {
437 int tmp = ntohl(server_ip.s_addr);
3861aa5c 438
232c150a
WD
439 *e++ = 54; /* ServerID */
440 *e++ = 4;
441 *e++ = tmp >> 24;
442 *e++ = tmp >> 16;
443 *e++ = tmp >> 8;
444 *e++ = tmp & 0xff;
445 }
3861aa5c 446
049a95a7
JH
447 if (requested_ip.s_addr) {
448 int tmp = ntohl(requested_ip.s_addr);
3861aa5c 449
232c150a
WD
450 *e++ = 50; /* Requested IP */
451 *e++ = 4;
452 *e++ = tmp >> 24;
453 *e++ = tmp >> 16;
454 *e++ = tmp >> 8;
455 *e++ = tmp & 0xff;
456 }
1fe80d79 457#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
3090b7e3
JH
458 hostname = getenv("hostname");
459 if (hostname) {
460 int hostnamelen = strlen(hostname);
232c150a
WD
461
462 *e++ = 12; /* Hostname */
463 *e++ = hostnamelen;
3090b7e3 464 memcpy(e, hostname, hostnamelen);
232c150a
WD
465 e += hostnamelen;
466 }
fe389a82
SR
467#endif
468
d2b5d5c4
JH
469#if defined(CONFIG_BOOTP_PXE)
470 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
471 *e++ = 93; /* Client System Architecture */
472 *e++ = 2;
473 *e++ = (clientarch >> 8) & 0xff;
474 *e++ = clientarch & 0xff;
475
476 *e++ = 94; /* Client Network Interface Identifier */
477 *e++ = 3;
478 *e++ = 1; /* type field for UNDI */
479 *e++ = 0; /* major revision */
480 *e++ = 0; /* minor revision */
481
482 uuid = getenv("pxeuuid");
483
484 if (uuid) {
485 if (uuid_str_valid(uuid)) {
486 *e++ = 97; /* Client Machine Identifier */
487 *e++ = 17;
488 *e++ = 0; /* type 0 - UUID */
489
d718ded0 490 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
d2b5d5c4
JH
491 e += 16;
492 } else {
493 printf("Invalid pxeuuid: %s\n", uuid);
494 }
495 }
9ace17c8 496#endif
d2b5d5c4 497
9ace17c8
IY
498#ifdef CONFIG_BOOTP_VCI_STRING
499 put_vci(e, CONFIG_BOOTP_VCI_STRING);
d2b5d5c4
JH
500#endif
501
1fe80d79 502#if defined(CONFIG_BOOTP_VENDOREX)
3090b7e3
JH
503 x = dhcp_vendorex_prep(e);
504 if (x)
232c150a 505 return x - start;
3861aa5c
WD
506#endif
507
232c150a
WD
508 *e++ = 55; /* Parameter Request List */
509 cnt = e++; /* Pointer to count of requested items */
510 *cnt = 0;
1fe80d79 511#if defined(CONFIG_BOOTP_SUBNETMASK)
232c150a
WD
512 *e++ = 1; /* Subnet Mask */
513 *cnt += 1;
3861aa5c 514#endif
1fe80d79 515#if defined(CONFIG_BOOTP_TIMEOFFSET)
ea287deb
WD
516 *e++ = 2;
517 *cnt += 1;
518#endif
1fe80d79 519#if defined(CONFIG_BOOTP_GATEWAY)
232c150a
WD
520 *e++ = 3; /* Router Option */
521 *cnt += 1;
3861aa5c 522#endif
1fe80d79 523#if defined(CONFIG_BOOTP_DNS)
232c150a
WD
524 *e++ = 6; /* DNS Server(s) */
525 *cnt += 1;
3861aa5c 526#endif
1fe80d79 527#if defined(CONFIG_BOOTP_HOSTNAME)
232c150a
WD
528 *e++ = 12; /* Hostname */
529 *cnt += 1;
3861aa5c 530#endif
1fe80d79 531#if defined(CONFIG_BOOTP_BOOTFILESIZE)
232c150a
WD
532 *e++ = 13; /* Boot File Size */
533 *cnt += 1;
3861aa5c 534#endif
1fe80d79 535#if defined(CONFIG_BOOTP_BOOTPATH)
232c150a
WD
536 *e++ = 17; /* Boot path */
537 *cnt += 1;
3861aa5c 538#endif
1fe80d79 539#if defined(CONFIG_BOOTP_NISDOMAIN)
232c150a
WD
540 *e++ = 40; /* NIS Domain name request */
541 *cnt += 1;
ea287deb 542#endif
1fe80d79 543#if defined(CONFIG_BOOTP_NTPSERVER)
ea287deb
WD
544 *e++ = 42;
545 *cnt += 1;
3861aa5c 546#endif
258ccd68
JL
547 /* no options, so back up to avoid sending an empty request list */
548 if (*cnt == 0)
549 e -= 2;
550
232c150a 551 *e++ = 255; /* End of the list */
3861aa5c 552
232c150a 553 /* Pad to minimal length */
3861aa5c 554#ifdef CONFIG_DHCP_MIN_EXT_LEN
21076f61 555 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
232c150a 556 *e++ = 0;
3861aa5c
WD
557#endif
558
232c150a 559 return e - start;
3861aa5c
WD
560}
561
610f2e9c 562#else
3861aa5c 563/*
3090b7e3 564 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
3861aa5c 565 */
049a95a7 566static int bootp_extended(u8 *e)
3861aa5c 567{
232c150a 568 u8 *start = e;
3861aa5c 569
232c150a
WD
570 *e++ = 99; /* RFC1048 Magic Cookie */
571 *e++ = 130;
572 *e++ = 83;
573 *e++ = 99;
3861aa5c 574
643d1ab2 575#if defined(CONFIG_CMD_DHCP)
232c150a
WD
576 *e++ = 53; /* DHCP Message Type */
577 *e++ = 1;
578 *e++ = DHCP_DISCOVER;
579
580 *e++ = 57; /* Maximum DHCP Message Size */
581 *e++ = 2;
f8315731
JH
582 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
583 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
610f2e9c 584#endif
3861aa5c 585
7ac2fe2d
IY
586#if defined(CONFIG_BOOTP_VCI_STRING) || \
587 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
588#ifdef CONFIG_SPL_BUILD
589 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
590#else
9ace17c8
IY
591 put_vci(e, CONFIG_BOOTP_VCI_STRING);
592#endif
7ac2fe2d 593#endif
9ace17c8 594
1fe80d79 595#if defined(CONFIG_BOOTP_SUBNETMASK)
232c150a
WD
596 *e++ = 1; /* Subnet mask request */
597 *e++ = 4;
598 e += 4;
3861aa5c
WD
599#endif
600
1fe80d79 601#if defined(CONFIG_BOOTP_GATEWAY)
232c150a
WD
602 *e++ = 3; /* Default gateway request */
603 *e++ = 4;
604 e += 4;
3861aa5c
WD
605#endif
606
1fe80d79 607#if defined(CONFIG_BOOTP_DNS)
232c150a
WD
608 *e++ = 6; /* Domain Name Server */
609 *e++ = 4;
610 e += 4;
3861aa5c
WD
611#endif
612
1fe80d79 613#if defined(CONFIG_BOOTP_HOSTNAME)
232c150a
WD
614 *e++ = 12; /* Host name request */
615 *e++ = 32;
616 e += 32;
3861aa5c
WD
617#endif
618
1fe80d79 619#if defined(CONFIG_BOOTP_BOOTFILESIZE)
232c150a
WD
620 *e++ = 13; /* Boot file size */
621 *e++ = 2;
622 e += 2;
3861aa5c
WD
623#endif
624
1fe80d79 625#if defined(CONFIG_BOOTP_BOOTPATH)
232c150a
WD
626 *e++ = 17; /* Boot path */
627 *e++ = 32;
628 e += 32;
3861aa5c
WD
629#endif
630
1fe80d79 631#if defined(CONFIG_BOOTP_NISDOMAIN)
232c150a
WD
632 *e++ = 40; /* NIS Domain name request */
633 *e++ = 32;
634 e += 32;
3861aa5c 635#endif
09e3a67d
LP
636#if defined(CONFIG_BOOTP_NTPSERVER)
637 *e++ = 42;
638 *e++ = 4;
639 e += 4;
640#endif
3861aa5c 641
232c150a 642 *e++ = 255; /* End of the list */
3861aa5c 643
232c150a 644 return e - start;
3861aa5c 645}
610f2e9c 646#endif
3861aa5c 647
f59be6e8
SW
648void BootpReset(void)
649{
92ac8acc 650 bootp_num_ids = 0;
f59be6e8
SW
651 BootpTry = 0;
652 bootp_start = get_timer(0);
92ac8acc 653 bootp_timeout = 250;
f59be6e8
SW
654}
655
3861aa5c 656void
3090b7e3 657BootpRequest(void)
3861aa5c 658{
db288a96 659 uchar *pkt, *iphdr;
3090b7e3 660 struct Bootp_t *bp;
ae446f56
JH
661 int extlen, pktlen, iplen;
662 int eth_hdr_size;
eafc8db0 663#ifdef CONFIG_BOOTP_RANDOM_DELAY
8e8d73b4 664 ulong rand_ms;
eafc8db0 665#endif
92ac8acc 666 ulong BootpID;
049a95a7
JH
667 struct in_addr zero_ip;
668 struct in_addr bcast_ip;
3861aa5c 669
573f14fe 670 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
643d1ab2 671#if defined(CONFIG_CMD_DHCP)
3861aa5c
WD
672 dhcp_state = INIT;
673#endif
674
675#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
eafc8db0
JH
676 if (BootpTry == 0)
677 srand_mac();
3861aa5c 678
eafc8db0
JH
679 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
680 rand_ms = rand() >> (22 - BootpTry);
681 else /* After 3rd BOOTP request max 8192 * 1ms */
682 rand_ms = rand() >> 19;
3861aa5c 683
eafc8db0 684 printf("Random delay: %ld ms...\n", rand_ms);
8e8d73b4 685 mdelay(rand_ms);
3090b7e3 686
3861aa5c
WD
687#endif /* CONFIG_BOOTP_RANDOM_DELAY */
688
689 printf("BOOTP broadcast %d\n", ++BootpTry);
690 pkt = NetTxPacket;
3090b7e3 691 memset((void *)pkt, 0, PKTSIZE);
3861aa5c 692
ae446f56
JH
693 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
694 pkt += eth_hdr_size;
3861aa5c
WD
695
696 /*
3090b7e3
JH
697 * Next line results in incorrect packet size being transmitted,
698 * resulting in errors in some DHCP servers, reporting missing bytes.
699 * Size must be set in packet header after extension length has been
700 * determined.
3861aa5c
WD
701 * C. Hallinan, DS4.COM, Inc.
702 */
4b11c916 703 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
3090b7e3 704 sizeof (struct Bootp_t)); */
4b11c916 705 iphdr = pkt; /* We need this later for net_set_udp_header() */
594c26f8 706 pkt += IP_UDP_HDR_SIZE;
3861aa5c 707
3090b7e3 708 bp = (struct Bootp_t *)pkt;
3861aa5c
WD
709 bp->bp_op = OP_BOOTREQUEST;
710 bp->bp_htype = HWT_ETHER;
711 bp->bp_hlen = HWL_ETHER;
712 bp->bp_hops = 0;
49f3bdbb 713 bp->bp_secs = htons(get_timer(0) / 1000);
049a95a7
JH
714 zero_ip.s_addr = 0;
715 net_write_ip(&bp->bp_ciaddr, zero_ip);
716 net_write_ip(&bp->bp_yiaddr, zero_ip);
717 net_write_ip(&bp->bp_siaddr, zero_ip);
718 net_write_ip(&bp->bp_giaddr, zero_ip);
3090b7e3 719 memcpy(bp->bp_chaddr, NetOurEther, 6);
1411157d 720 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
3861aa5c
WD
721
722 /* Request additional information from the BOOTP/DHCP server */
643d1ab2 723#if defined(CONFIG_CMD_DHCP)
049a95a7
JH
724 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
725 zero_ip);
3861aa5c 726#else
049a95a7 727 extlen = bootp_extended((u8 *)bp->bp_vend);
610f2e9c 728#endif
3861aa5c
WD
729
730 /*
731 * Bootp ID is the lower 4 bytes of our ethernet address
49f3bdbb 732 * plus the current time in ms.
3861aa5c
WD
733 */
734 BootpID = ((ulong)NetOurEther[2] << 24)
735 | ((ulong)NetOurEther[3] << 16)
736 | ((ulong)NetOurEther[4] << 8)
737 | (ulong)NetOurEther[5];
738 BootpID += get_timer(0);
92ac8acc
TR
739 BootpID = htonl(BootpID);
740 bootp_add_id(BootpID);
3861aa5c
WD
741 NetCopyLong(&bp->bp_id, &BootpID);
742
743 /*
744 * Calculate proper packet lengths taking into account the
745 * variable size of the options field
746 */
ae446f56
JH
747 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
748 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
049a95a7
JH
749 bcast_ip.s_addr = 0xFFFFFFFFL;
750 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
f59be6e8 751 NetSetTimeout(bootp_timeout, BootpTimeout);
3861aa5c 752
643d1ab2 753#if defined(CONFIG_CMD_DHCP)
3861aa5c 754 dhcp_state = SELECTING;
049a95a7 755 net_set_udp_handler(dhcp_handler);
3861aa5c 756#else
049a95a7 757 net_set_udp_handler(bootp_handler);
610f2e9c 758#endif
3861aa5c
WD
759 NetSendPacket(NetTxPacket, pktlen);
760}
761
643d1ab2 762#if defined(CONFIG_CMD_DHCP)
3090b7e3 763static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
3861aa5c 764{
3e38691e 765 uchar *end = popt + BOOTP_HDR_SIZE;
3861aa5c 766 int oplen, size;
d8d8724b
WD
767#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
768 int *to_ptr;
769#endif
3861aa5c 770
232c150a 771 while (popt < end && *popt != 0xff) {
3861aa5c 772 oplen = *(popt + 1);
232c150a
WD
773 switch (*popt) {
774 case 1:
049a95a7 775 net_copy_ip(&net_netmask, (popt + 2));
232c150a 776 break;
1fe80d79 777#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
ea287deb 778 case 2: /* Time offset */
d8d8724b 779 to_ptr = &NetTimeOffset;
3090b7e3
JH
780 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
781 NetTimeOffset = ntohl(NetTimeOffset);
ea287deb
WD
782 break;
783#endif
232c150a 784 case 3:
049a95a7 785 net_copy_ip(&net_gateway, (popt + 2));
232c150a
WD
786 break;
787 case 6:
049a95a7 788 net_copy_ip(&net_dns_server, (popt + 2));
1fe80d79 789#if defined(CONFIG_BOOTP_DNS2)
3090b7e3 790 if (*(popt + 1) > 4)
049a95a7 791 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
fe389a82 792#endif
232c150a
WD
793 break;
794 case 12:
3090b7e3
JH
795 size = truncate_sz("Host Name",
796 sizeof(NetOurHostName), oplen);
797 memcpy(&NetOurHostName, popt + 2, size);
232c150a
WD
798 NetOurHostName[size] = 0;
799 break;
800 case 15: /* Ignore Domain Name Option */
801 break;
802 case 17:
3090b7e3
JH
803 size = truncate_sz("Root Path",
804 sizeof(NetOurRootPath), oplen);
805 memcpy(&NetOurRootPath, popt + 2, size);
232c150a
WD
806 NetOurRootPath[size] = 0;
807 break;
ee0f60df
BR
808 case 28: /* Ignore Broadcast Address Option */
809 break;
1fe80d79 810#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
ea287deb 811 case 42: /* NTP server IP */
049a95a7 812 net_copy_ip(&net_ntp_server, (popt + 2));
ea287deb
WD
813 break;
814#endif
232c150a 815 case 51:
3090b7e3 816 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
232c150a
WD
817 break;
818 case 53: /* Ignore Message Type Option */
819 break;
820 case 54:
049a95a7 821 net_copy_ip(&dhcp_server_ip, (popt + 2));
232c150a
WD
822 break;
823 case 58: /* Ignore Renewal Time Option */
824 break;
825 case 59: /* Ignore Rebinding Time Option */
826 break;
3b2e4fd9
WD
827 case 66: /* Ignore TFTP server name */
828 break;
829 case 67: /* vendor opt bootfile */
830 /*
831 * I can't use dhcp_vendorex_proc here because I need
832 * to write into the bootp packet - even then I had to
833 * pass the bootp packet pointer into here as the
834 * second arg
835 */
3090b7e3 836 size = truncate_sz("Opt Boot File",
3b2e4fd9
WD
837 sizeof(bp->bp_file),
838 oplen);
839 if (bp->bp_file[0] == '\0' && size > 0) {
840 /*
841 * only use vendor boot file if we didn't
842 * receive a boot file in the main non-vendor
843 * part of the packet - god only knows why
844 * some vendors chose not to use this perfectly
845 * good spot to store the boot file (join on
846 * Tru64 Unix) it seems mind bogglingly crazy
847 * to me
848 */
849 printf("*** WARNING: using vendor "
850 "optional boot file\n");
851 memcpy(bp->bp_file, popt + 2, size);
852 bp->bp_file[size] = '\0';
853 }
854 break;
232c150a 855 default:
1fe80d79 856#if defined(CONFIG_BOOTP_VENDOREX)
3090b7e3 857 if (dhcp_vendorex_proc(popt))
8bde7f77 858 break;
3861aa5c 859#endif
3090b7e3
JH
860 printf("*** Unhandled DHCP Option in OFFER/ACK:"
861 " %d\n", *popt);
232c150a 862 break;
3861aa5c
WD
863 }
864 popt += oplen + 2; /* Process next option */
865 }
866}
867
868static int DhcpMessageType(unsigned char *popt)
869{
3090b7e3 870 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
3861aa5c
WD
871 return -1;
872
873 popt += 4;
3090b7e3
JH
874 while (*popt != 0xff) {
875 if (*popt == 53) /* DHCP Message Type */
3861aa5c
WD
876 return *(popt + 2);
877 popt += *(popt + 1) + 2; /* Scan through all options */
878 }
879 return -1;
880}
881
3090b7e3 882static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
3861aa5c 883{
db288a96 884 uchar *pkt, *iphdr;
3090b7e3 885 struct Bootp_t *bp;
3861aa5c 886 int pktlen, iplen, extlen;
ae446f56 887 int eth_hdr_size;
049a95a7
JH
888 struct in_addr offered_ip;
889 struct in_addr zero_ip;
890 struct in_addr bcast_ip;
3861aa5c 891
0ebf04c6 892 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
3861aa5c 893 pkt = NetTxPacket;
3090b7e3 894 memset((void *)pkt, 0, PKTSIZE);
3861aa5c 895
ae446f56
JH
896 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
897 pkt += eth_hdr_size;
3861aa5c 898
3090b7e3 899 iphdr = pkt; /* We'll need this later to set proper pkt size */
594c26f8 900 pkt += IP_UDP_HDR_SIZE;
3861aa5c 901
3090b7e3 902 bp = (struct Bootp_t *)pkt;
3861aa5c
WD
903 bp->bp_op = OP_BOOTREQUEST;
904 bp->bp_htype = HWT_ETHER;
905 bp->bp_hlen = HWL_ETHER;
906 bp->bp_hops = 0;
49f3bdbb 907 bp->bp_secs = htons(get_timer(0) / 1000);
3090b7e3
JH
908 /* Do not set the client IP, your IP, or server IP yet, since it
909 * hasn't been ACK'ed by the server yet */
e5c794e4 910
c6686703 911 /*
d82718fe
WD
912 * RFC3046 requires Relay Agents to discard packets with
913 * nonzero and offered giaddr
914 */
049a95a7
JH
915 zero_ip.s_addr = 0;
916 net_write_ip(&bp->bp_giaddr, zero_ip);
d82718fe 917
3090b7e3 918 memcpy(bp->bp_chaddr, NetOurEther, 6);
3861aa5c
WD
919
920 /*
921 * ID is the id of the OFFER packet
922 */
923
924 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
925
926 /*
927 * Copy options from OFFER packet if present
928 */
e5c794e4
JF
929
930 /* Copy offered IP into the parameters request list */
049a95a7
JH
931 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
932 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
933 dhcp_server_ip, offered_ip);
3861aa5c 934
ae446f56
JH
935 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
936 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
049a95a7
JH
937 bcast_ip.s_addr = 0xFFFFFFFFL;
938 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
3861aa5c 939
d9a2f416
AV
940#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
941 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
942#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
f9623229 943 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
3861aa5c
WD
944 NetSendPacket(NetTxPacket, pktlen);
945}
946
947/*
948 * Handle DHCP received packets.
949 */
049a95a7
JH
950static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
951 unsigned src, unsigned len)
3861aa5c 952{
3090b7e3 953 struct Bootp_t *bp = (struct Bootp_t *)pkt;
3861aa5c 954
0ebf04c6 955 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
3861aa5c
WD
956 src, dest, len, dhcp_state);
957
3090b7e3
JH
958 /* Filter out pkts we don't want */
959 if (BootpCheckPkt(pkt, dest, src, len))
3861aa5c
WD
960 return;
961
3090b7e3
JH
962 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
963 " %d\n", src, dest, len, dhcp_state);
3861aa5c
WD
964
965 switch (dhcp_state) {
966 case SELECTING:
967 /*
968 * Wait an appropriate time for any potential DHCPOFFER packets
3090b7e3
JH
969 * to arrive. Then select one, and generate DHCPREQUEST
970 * response. If filename is in format we recognize, assume it
971 * is a valid OFFER from a server we want.
3861aa5c 972 */
0ebf04c6 973 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
6d0f6bcf 974#ifdef CONFIG_SYS_BOOTFILE_PREFIX
3861aa5c 975 if (strncmp(bp->bp_file,
6d0f6bcf 976 CONFIG_SYS_BOOTFILE_PREFIX,
3090b7e3 977 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
6d0f6bcf 978#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
3861aa5c 979
0ebf04c6 980 debug("TRANSITIONING TO REQUESTING STATE\n");
3861aa5c 981 dhcp_state = REQUESTING;
759a51b4 982
3090b7e3
JH
983 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
984 htonl(BOOTP_VENDOR_MAGIC))
3b2e4fd9 985 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
3861aa5c 986
f59be6e8 987 NetSetTimeout(5000, BootpTimeout);
3861aa5c 988 DhcpSendRequestPkt(bp);
6d0f6bcf 989#ifdef CONFIG_SYS_BOOTFILE_PREFIX
3861aa5c 990 }
6d0f6bcf 991#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
3861aa5c
WD
992
993 return;
994 break;
995 case REQUESTING:
0ebf04c6 996 debug("DHCP State: REQUESTING\n");
3861aa5c 997
3090b7e3
JH
998 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
999 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
1000 htonl(BOOTP_VENDOR_MAGIC))
3b2e4fd9 1001 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
3090b7e3
JH
1002 /* Store net params from reply */
1003 BootpCopyNetParams(bp);
3861aa5c 1004 dhcp_state = BOUND;
92ac8acc 1005 printf("DHCP client bound to address %pI4 (%lu ms)\n",
049a95a7 1006 &net_ip, get_timer(bootp_start));
573f14fe 1007 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
3090b7e3 1008 "bootp_stop");
3861aa5c 1009
e4a3d57d 1010 net_auto_load();
3861aa5c
WD
1011 return;
1012 }
1013 break;
51dfe138
RB
1014 case BOUND:
1015 /* DHCP client bound to address */
1016 break;
3861aa5c 1017 default:
3090b7e3 1018 puts("DHCP: INVALID STATE\n");
3861aa5c
WD
1019 break;
1020 }
1021
1022}
1023
1024void DhcpRequest(void)
1025{
1026 BootpRequest();
1027}
992742a5 1028#endif /* CONFIG_CMD_DHCP */