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