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