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