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