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