xref: /openbmc/u-boot/net/bootp.c (revision 6c0bf27d)
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 	TftpStart();
168 	}
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 
385 #if defined(CONFIG_BOOTP_VENDOREX)
386 	u8 *x;
387 #endif
388 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
389 	char *hostname;
390 #endif
391 
392 	*e++ = 99;		/* RFC1048 Magic Cookie */
393 	*e++ = 130;
394 	*e++ = 83;
395 	*e++ = 99;
396 
397 	*e++ = 53;		/* DHCP Message Type */
398 	*e++ = 1;
399 	*e++ = message_type;
400 
401 	*e++ = 57;		/* Maximum DHCP Message Size */
402 	*e++ = 2;
403 	*e++ = (576 - 312 + OPT_SIZE) >> 8;
404 	*e++ = (576 - 312 + OPT_SIZE) & 0xff;
405 
406 	if (ServerID) {
407 		int tmp = ntohl (ServerID);
408 
409 		*e++ = 54;	/* ServerID */
410 		*e++ = 4;
411 		*e++ = tmp >> 24;
412 		*e++ = tmp >> 16;
413 		*e++ = tmp >> 8;
414 		*e++ = tmp & 0xff;
415 	}
416 
417 	if (RequestedIP) {
418 		int tmp = ntohl (RequestedIP);
419 
420 		*e++ = 50;	/* Requested IP */
421 		*e++ = 4;
422 		*e++ = tmp >> 24;
423 		*e++ = tmp >> 16;
424 		*e++ = tmp >> 8;
425 		*e++ = tmp & 0xff;
426 	}
427 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
428 	if ((hostname = getenv ("hostname"))) {
429 		int hostnamelen = strlen (hostname);
430 
431 		*e++ = 12;	/* Hostname */
432 		*e++ = hostnamelen;
433 		memcpy (e, hostname, hostnamelen);
434 		e += hostnamelen;
435 	}
436 #endif
437 
438 #if defined(CONFIG_BOOTP_VENDOREX)
439 	if ((x = dhcp_vendorex_prep (e)))
440 		return x - start;
441 #endif
442 
443 	*e++ = 55;		/* Parameter Request List */
444 	 cnt = e++;		/* Pointer to count of requested items */
445 	*cnt = 0;
446 #if defined(CONFIG_BOOTP_SUBNETMASK)
447 	*e++  = 1;		/* Subnet Mask */
448 	*cnt += 1;
449 #endif
450 #if defined(CONFIG_BOOTP_TIMEOFFSET)
451 	*e++  = 2;
452 	*cnt += 1;
453 #endif
454 #if defined(CONFIG_BOOTP_GATEWAY)
455 	*e++  = 3;		/* Router Option */
456 	*cnt += 1;
457 #endif
458 #if defined(CONFIG_BOOTP_DNS)
459 	*e++  = 6;		/* DNS Server(s) */
460 	*cnt += 1;
461 #endif
462 #if defined(CONFIG_BOOTP_HOSTNAME)
463 	*e++  = 12;		/* Hostname */
464 	*cnt += 1;
465 #endif
466 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
467 	*e++  = 13;		/* Boot File Size */
468 	*cnt += 1;
469 #endif
470 #if defined(CONFIG_BOOTP_BOOTPATH)
471 	*e++  = 17;		/* Boot path */
472 	*cnt += 1;
473 #endif
474 #if defined(CONFIG_BOOTP_NISDOMAIN)
475 	*e++  = 40;		/* NIS Domain name request */
476 	*cnt += 1;
477 #endif
478 #if defined(CONFIG_BOOTP_NTPSERVER)
479 	*e++  = 42;
480 	*cnt += 1;
481 #endif
482 	/* no options, so back up to avoid sending an empty request list */
483 	if (*cnt == 0)
484 		e -= 2;
485 
486 	*e++  = 255;		/* End of the list */
487 
488 	/* Pad to minimal length */
489 #ifdef	CONFIG_DHCP_MIN_EXT_LEN
490 	while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
491 		*e++ = 0;
492 #endif
493 
494 	return e - start;
495 }
496 
497 #else
498 /*
499  *	Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
500  */
501 static int BootpExtended (u8 * e)
502 {
503 	u8 *start = e;
504 
505 	*e++ = 99;		/* RFC1048 Magic Cookie */
506 	*e++ = 130;
507 	*e++ = 83;
508 	*e++ = 99;
509 
510 #if defined(CONFIG_CMD_DHCP)
511 	*e++ = 53;		/* DHCP Message Type */
512 	*e++ = 1;
513 	*e++ = DHCP_DISCOVER;
514 
515 	*e++ = 57;		/* Maximum DHCP Message Size */
516 	*e++ = 2;
517 	*e++ = (576 - 312 + OPT_SIZE) >> 16;
518 	*e++ = (576 - 312 + OPT_SIZE) & 0xff;
519 #endif
520 
521 #if defined(CONFIG_BOOTP_SUBNETMASK)
522 	*e++ = 1;		/* Subnet mask request */
523 	*e++ = 4;
524 	e   += 4;
525 #endif
526 
527 #if defined(CONFIG_BOOTP_GATEWAY)
528 	*e++ = 3;		/* Default gateway request */
529 	*e++ = 4;
530 	e   += 4;
531 #endif
532 
533 #if defined(CONFIG_BOOTP_DNS)
534 	*e++ = 6;		/* Domain Name Server */
535 	*e++ = 4;
536 	e   += 4;
537 #endif
538 
539 #if defined(CONFIG_BOOTP_HOSTNAME)
540 	*e++ = 12;		/* Host name request */
541 	*e++ = 32;
542 	e   += 32;
543 #endif
544 
545 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
546 	*e++ = 13;		/* Boot file size */
547 	*e++ = 2;
548 	e   += 2;
549 #endif
550 
551 #if defined(CONFIG_BOOTP_BOOTPATH)
552 	*e++ = 17;		/* Boot path */
553 	*e++ = 32;
554 	e   += 32;
555 #endif
556 
557 #if defined(CONFIG_BOOTP_NISDOMAIN)
558 	*e++ = 40;		/* NIS Domain name request */
559 	*e++ = 32;
560 	e   += 32;
561 #endif
562 #if defined(CONFIG_BOOTP_NTPSERVER)
563 	*e++ = 42;
564 	*e++ = 4;
565 	e   += 4;
566 #endif
567 
568 	*e++ = 255;		/* End of the list */
569 
570 	return e - start;
571 }
572 #endif
573 
574 void
575 BootpRequest (void)
576 {
577 	volatile uchar *pkt, *iphdr;
578 	Bootp_t *bp;
579 	int ext_len, pktlen, iplen;
580 
581 #if defined(CONFIG_CMD_DHCP)
582 	dhcp_state = INIT;
583 #endif
584 
585 #ifdef CONFIG_BOOTP_RANDOM_DELAY		/* Random BOOTP delay */
586 	unsigned char bi_enetaddr[6];
587 	int   reg;
588 	ulong tst1, tst2, sum, m_mask, m_value = 0;
589 
590 	if (BootpTry ==0) {
591 		/* get our mac */
592 		eth_getenv_enetaddr("ethaddr", bi_enetaddr);
593 
594 		debug("BootpRequest => Our Mac: ");
595 		for (reg=0; reg<6; reg++)
596 			debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
597 
598 		/* Mac-Manipulation 2 get seed1 */
599 		tst1=0;
600 		tst2=0;
601 		for (reg=2; reg<6; reg++) {
602 			tst1 = tst1 << 8;
603 			tst1 = tst1 | bi_enetaddr[reg];
604 		}
605 		for (reg=0; reg<2; reg++) {
606 			tst2 = tst2 | bi_enetaddr[reg];
607 			tst2 = tst2 << 8;
608 		}
609 
610 		seed1 = tst1^tst2;
611 
612 		/* Mirror seed1*/
613 		m_mask=0x1;
614 		for (reg=1;reg<=32;reg++) {
615 			m_value |= (m_mask & seed1);
616 			seed1 = seed1 >> 1;
617 			m_value = m_value << 1;
618 		}
619 		seed1 = m_value;
620 		seed2 = 0xB78D0945;
621 	}
622 
623 	/* Random Number Generator */
624 
625 	for (reg=0;reg<=0;reg++) {
626 		sum = seed1 + seed2;
627 		if (sum < seed1 || sum < seed2)
628 			sum++;
629 		seed2 = seed1;
630 		seed1 = sum;
631 
632 		if (BootpTry<=2) {	/* Start with max 1024 * 1ms */
633 			sum = sum >> (22-BootpTry);
634 		} else {		/*After 3rd BOOTP request max 8192 * 1ms */
635 			sum = sum >> 19;
636 		}
637 	}
638 
639 	printf ("Random delay: %ld ms...\n", sum);
640 	for (reg=0; reg <sum; reg++) {
641 		udelay(1000); /*Wait 1ms*/
642 	}
643 #endif	/* CONFIG_BOOTP_RANDOM_DELAY */
644 
645 	printf("BOOTP broadcast %d\n", ++BootpTry);
646 	pkt = NetTxPacket;
647 	memset ((void*)pkt, 0, PKTSIZE);
648 
649 	pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
650 
651 	/*
652 	 * Next line results in incorrect packet size being transmitted, resulting
653 	 * in errors in some DHCP servers, reporting missing bytes.  Size must be
654 	 * set in packet header after extension length has been determined.
655 	 * C. Hallinan, DS4.COM, Inc.
656 	 */
657 	/* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
658 	iphdr = pkt;	/* We need this later for NetSetIP() */
659 	pkt += IP_HDR_SIZE;
660 
661 	bp = (Bootp_t *)pkt;
662 	bp->bp_op = OP_BOOTREQUEST;
663 	bp->bp_htype = HWT_ETHER;
664 	bp->bp_hlen = HWL_ETHER;
665 	bp->bp_hops = 0;
666 	bp->bp_secs = htons(get_timer(0) / 1000);
667 	NetWriteIP(&bp->bp_ciaddr, 0);
668 	NetWriteIP(&bp->bp_yiaddr, 0);
669 	NetWriteIP(&bp->bp_siaddr, 0);
670 	NetWriteIP(&bp->bp_giaddr, 0);
671 	memcpy (bp->bp_chaddr, NetOurEther, 6);
672 	copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
673 
674 	/* Request additional information from the BOOTP/DHCP server */
675 #if defined(CONFIG_CMD_DHCP)
676 	ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
677 #else
678 	ext_len = BootpExtended((u8 *)bp->bp_vend);
679 #endif
680 
681 	/*
682 	 *	Bootp ID is the lower 4 bytes of our ethernet address
683 	 *	plus the current time in ms.
684 	 */
685 	BootpID = ((ulong)NetOurEther[2] << 24)
686 		| ((ulong)NetOurEther[3] << 16)
687 		| ((ulong)NetOurEther[4] << 8)
688 		| (ulong)NetOurEther[5];
689 	BootpID += get_timer(0);
690 	BootpID	 = htonl(BootpID);
691 	NetCopyLong(&bp->bp_id, &BootpID);
692 
693 	/*
694 	 * Calculate proper packet lengths taking into account the
695 	 * variable size of the options field
696 	 */
697 	pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
698 	iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
699 	NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
700 	NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
701 
702 #if defined(CONFIG_CMD_DHCP)
703 	dhcp_state = SELECTING;
704 	NetSetHandler(DhcpHandler);
705 #else
706 	NetSetHandler(BootpHandler);
707 #endif
708 	NetSendPacket(NetTxPacket, pktlen);
709 }
710 
711 #if defined(CONFIG_CMD_DHCP)
712 static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
713 {
714 	uchar *end = popt + BOOTP_HDR_SIZE;
715 	int oplen, size;
716 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
717 	int *to_ptr;
718 #endif
719 
720 	while (popt < end && *popt != 0xff) {
721 		oplen = *(popt + 1);
722 		switch (*popt) {
723 		case 1:
724 			NetCopyIP (&NetOurSubnetMask, (popt + 2));
725 			break;
726 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
727 		case 2:		/* Time offset	*/
728 			to_ptr = &NetTimeOffset;
729 			NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
730 			NetTimeOffset = ntohl (NetTimeOffset);
731 			break;
732 #endif
733 		case 3:
734 			NetCopyIP (&NetOurGatewayIP, (popt + 2));
735 			break;
736 		case 6:
737 			NetCopyIP (&NetOurDNSIP, (popt + 2));
738 #if defined(CONFIG_BOOTP_DNS2)
739 			if (*(popt + 1) > 4) {
740 				NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
741 			}
742 #endif
743 			break;
744 		case 12:
745 			size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
746 			memcpy (&NetOurHostName, popt + 2, size);
747 			NetOurHostName[size] = 0;
748 			break;
749 		case 15:	/* Ignore Domain Name Option */
750 			break;
751 		case 17:
752 			size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
753 			memcpy (&NetOurRootPath, popt + 2, size);
754 			NetOurRootPath[size] = 0;
755 			break;
756 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
757 		case 42:	/* NTP server IP */
758 			NetCopyIP (&NetNtpServerIP, (popt + 2));
759 			break;
760 #endif
761 		case 51:
762 			NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
763 			break;
764 		case 53:	/* Ignore Message Type Option */
765 			break;
766 		case 54:
767 			NetCopyIP (&NetDHCPServerIP, (popt + 2));
768 			break;
769 		case 58:	/* Ignore Renewal Time Option */
770 			break;
771 		case 59:	/* Ignore Rebinding Time Option */
772 			break;
773 		case 66:	/* Ignore TFTP server name */
774 			break;
775 		case 67:	/* vendor opt bootfile */
776 			/*
777 			 * I can't use dhcp_vendorex_proc here because I need
778 			 * to write into the bootp packet - even then I had to
779 			 * pass the bootp packet pointer into here as the
780 			 * second arg
781 			 */
782 			size = truncate_sz ("Opt Boot File",
783 					    sizeof(bp->bp_file),
784 					    oplen);
785 			if (bp->bp_file[0] == '\0' && size > 0) {
786 				/*
787 				 * only use vendor boot file if we didn't
788 				 * receive a boot file in the main non-vendor
789 				 * part of the packet - god only knows why
790 				 * some vendors chose not to use this perfectly
791 				 * good spot to store the boot file (join on
792 				 * Tru64 Unix) it seems mind bogglingly crazy
793 				 * to me
794 				 */
795 				printf("*** WARNING: using vendor "
796 					"optional boot file\n");
797 				memcpy(bp->bp_file, popt + 2, size);
798 				bp->bp_file[size] = '\0';
799 			}
800 			break;
801 		default:
802 #if defined(CONFIG_BOOTP_VENDOREX)
803 			if (dhcp_vendorex_proc (popt))
804 				break;
805 #endif
806 			printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
807 			break;
808 		}
809 		popt += oplen + 2;	/* Process next option */
810 	}
811 }
812 
813 static int DhcpMessageType(unsigned char *popt)
814 {
815 	if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
816 		return -1;
817 
818 	popt += 4;
819 	while ( *popt != 0xff ) {
820 		if ( *popt == 53 )	/* DHCP Message Type */
821 			return *(popt + 2);
822 		popt += *(popt + 1) + 2;	/* Scan through all options */
823 	}
824 	return -1;
825 }
826 
827 static void DhcpSendRequestPkt(Bootp_t *bp_offer)
828 {
829 	volatile uchar *pkt, *iphdr;
830 	Bootp_t *bp;
831 	int pktlen, iplen, extlen;
832 	IPaddr_t OfferedIP;
833 
834 	debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
835 	pkt = NetTxPacket;
836 	memset ((void*)pkt, 0, PKTSIZE);
837 
838 	pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
839 
840 	iphdr = pkt;		/* We'll need this later to set proper pkt size */
841 	pkt += IP_HDR_SIZE;
842 
843 	bp = (Bootp_t *)pkt;
844 	bp->bp_op = OP_BOOTREQUEST;
845 	bp->bp_htype = HWT_ETHER;
846 	bp->bp_hlen = HWL_ETHER;
847 	bp->bp_hops = 0;
848 	bp->bp_secs = htons(get_timer(0) / 1000);
849 	/* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
850 	 * the server yet */
851 
852 	/*
853 	 * RFC3046 requires Relay Agents to discard packets with
854 	 * nonzero and offered giaddr
855 	 */
856 	NetWriteIP(&bp->bp_giaddr, 0);
857 
858 	memcpy (bp->bp_chaddr, NetOurEther, 6);
859 
860 	/*
861 	 * ID is the id of the OFFER packet
862 	 */
863 
864 	NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
865 
866 	/*
867 	 * Copy options from OFFER packet if present
868 	 */
869 
870 	/* Copy offered IP into the parameters request list */
871 	NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
872 	extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
873 
874 	pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
875 	iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
876 	NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
877 
878 	debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
879 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
880 	udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
881 #endif	/* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
882 	NetSendPacket(NetTxPacket, pktlen);
883 }
884 
885 /*
886  *	Handle DHCP received packets.
887  */
888 static void
889 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
890 	    unsigned len)
891 {
892 	Bootp_t *bp = (Bootp_t *)pkt;
893 
894 	debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
895 		src, dest, len, dhcp_state);
896 
897 	if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
898 		return;
899 
900 	debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
901 		src, dest, len, dhcp_state);
902 
903 	switch (dhcp_state) {
904 	case SELECTING:
905 		/*
906 		 * Wait an appropriate time for any potential DHCPOFFER packets
907 		 * to arrive.  Then select one, and generate DHCPREQUEST response.
908 		 * If filename is in format we recognize, assume it is a valid
909 		 * OFFER from a server we want.
910 		 */
911 		debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
912 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
913 		if (strncmp(bp->bp_file,
914 			    CONFIG_SYS_BOOTFILE_PREFIX,
915 			    strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
916 #endif	/* CONFIG_SYS_BOOTFILE_PREFIX */
917 
918 			debug("TRANSITIONING TO REQUESTING STATE\n");
919 			dhcp_state = REQUESTING;
920 
921 			if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
922 				DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
923 
924 			NetSetTimeout(TIMEOUT, BootpTimeout);
925 			DhcpSendRequestPkt(bp);
926 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
927 		}
928 #endif	/* CONFIG_SYS_BOOTFILE_PREFIX */
929 
930 		return;
931 		break;
932 	case REQUESTING:
933 		debug("DHCP State: REQUESTING\n");
934 
935 		if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
936 			if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
937 				DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
938 			BootpCopyNetParams(bp); /* Store net params from reply */
939 			dhcp_state = BOUND;
940 			printf ("DHCP client bound to address %pI4\n", &NetOurIP);
941 
942 			auto_load();
943 			return;
944 		}
945 		break;
946 	case BOUND:
947 		/* DHCP client bound to address */
948 		break;
949 	default:
950 		puts ("DHCP: INVALID STATE\n");
951 		break;
952 	}
953 
954 }
955 
956 void DhcpRequest(void)
957 {
958 	BootpRequest();
959 }
960 #endif	/* CONFIG_CMD_DHCP */
961