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