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