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