2 * Based on LiMon - BOOTP.
4 * Copyright 1994, 1995, 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
17 #ifdef CONFIG_STATUS_LED
18 #include <status_led.h>
21 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
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 */
27 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
30 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
31 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
33 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
34 #define CONFIG_DHCP_MIN_EXT_LEN 64
39 #ifdef CONFIG_BOOTP_RANDOM_DELAY
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,
52 static char *dhcpmsg2str(int 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;
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 */
74 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
76 Bootp_t *bp = (Bootp_t *) pkt;
79 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
81 else if (len < sizeof (Bootp_t) - OPT_SIZE)
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 ) {
90 else if (bp->bp_htype != HWT_ETHER)
92 else if (bp->bp_hlen != HWL_ETHER)
94 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
98 debug("Filtering pkt = %d\n", retval);
104 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
106 static void BootpCopyNetParams(Bootp_t *bp)
110 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
111 #if !defined(CONFIG_BOOTP_SERVERIP)
112 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
114 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
115 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
117 if (strlen(bp->bp_file) > 0)
118 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
120 debug("Bootfile: %s\n", BootFile);
122 /* Propagate to environment:
123 * don't delete exising entry when BOOTP / DHCP reply does
124 * not contain a new value
127 setenv ("bootfile", BootFile);
131 static int truncate_sz (const char *name, int maxlen, int curlen)
133 if (curlen >= maxlen) {
134 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
135 name, curlen, maxlen);
142 * Check if autoload is enabled. If so, use either NFS or TFTP to download
145 static void auto_load(void)
147 const char *s = getenv("autoload");
152 * Just use BOOTP to configure system;
153 * Do not use TFTP to load the bootfile.
155 NetState = NETLOOP_SUCCESS;
158 #if defined(CONFIG_CMD_NFS)
159 if (strcmp(s, "NFS") == 0) {
161 * Use NFS to load the bootfile.
171 #if !defined(CONFIG_CMD_DHCP)
173 static void BootpVendorFieldProcess (u8 * ext)
175 int size = *(ext + 1);
177 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
183 /* Fixed length fields */
184 case 1: /* Subnet mask */
185 if (NetOurSubnetMask == 0)
186 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
188 case 2: /* Time offset - Not yet supported */
190 /* Variable length fields */
191 case 3: /* Gateways list */
192 if (NetOurGatewayIP == 0) {
193 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
196 case 4: /* Time server - Not yet supported */
198 case 5: /* IEN-116 name server - Not yet supported */
201 if (NetOurDNSIP == 0) {
202 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
204 #if defined(CONFIG_BOOTP_DNS2)
205 if ((NetOurDNS2IP == 0) && (size > 4)) {
206 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
210 case 7: /* Log server - Not yet supported */
212 case 8: /* Cookie/Quote server - Not yet supported */
214 case 9: /* LPR server - Not yet supported */
216 case 10: /* Impress server - Not yet supported */
218 case 11: /* RPL server - Not yet supported */
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;
227 case 13: /* Boot file size */
229 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
231 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
233 case 14: /* Merit dump file - Not yet supported */
235 case 15: /* Domain name - Not yet supported */
237 case 16: /* Swap server - Not yet supported */
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;
246 case 18: /* Extension path - Not yet supported */
248 * This can be used to send the information of the
249 * vendor area in another file that the client can
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;
261 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
262 case 42: /* NTP server IP */
263 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
266 /* Application layer fields */
267 case 43: /* Vendor specific info - Not yet supported */
269 * Binary information to exchange specific
270 * product information.
273 /* Reserved (custom) fields (128..254) */
277 static void BootpVendorProcess (u8 * ext, int size)
279 u8 *end = ext + size;
281 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
283 while ((ext < end) && (*ext != 0xff)) {
291 BootpVendorFieldProcess (opt);
295 debug("[BOOTP] Received fields: \n");
296 if (NetOurSubnetMask)
297 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
300 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
303 debug("NetBootFileSize : %d\n", NetBootFileSize);
305 if (NetOurHostName[0])
306 debug("NetOurHostName : %s\n", NetOurHostName);
308 if (NetOurRootPath[0])
309 debug("NetOurRootPath : %s\n", NetOurRootPath);
311 if (NetOurNISDomain[0])
312 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
315 debug("NetBootFileSize: %d\n", NetBootFileSize);
317 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
319 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
324 * Handle a BOOTP received packet.
327 BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
332 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
333 src, dest, len, sizeof (Bootp_t));
337 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
341 * Got a good BOOTP reply. Copy the data into our variables.
343 #ifdef CONFIG_STATUS_LED
344 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
347 BootpCopyNetParams(bp); /* Store net parameters from reply */
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);
353 NetSetTimeout(0, (thand_f *)0);
355 debug("Got good BOOTP\n");
362 * Timeout on BOOTP/DHCP request.
367 if (BootpTry >= TIMEOUT_COUNT) {
368 puts ("\nRetry count exceeded; starting again\n");
371 NetSetTimeout (TIMEOUT, BootpTimeout);
377 * Initialize BOOTP extension fields in the request.
379 #if defined(CONFIG_CMD_DHCP)
380 static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
384 #if defined(CONFIG_BOOTP_PXE)
390 #if defined(CONFIG_BOOTP_VENDOREX)
393 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
397 *e++ = 99; /* RFC1048 Magic Cookie */
402 *e++ = 53; /* DHCP Message Type */
406 *e++ = 57; /* Maximum DHCP Message Size */
408 *e++ = (576 - 312 + OPT_SIZE) >> 8;
409 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
412 int tmp = ntohl (ServerID);
414 *e++ = 54; /* ServerID */
423 int tmp = ntohl (RequestedIP);
425 *e++ = 50; /* Requested IP */
432 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
433 if ((hostname = getenv ("hostname"))) {
434 int hostnamelen = strlen (hostname);
436 *e++ = 12; /* Hostname */
438 memcpy (e, hostname, hostnamelen);
443 #if defined(CONFIG_BOOTP_PXE)
444 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
445 *e++ = 93; /* Client System Architecture */
447 *e++ = (clientarch >> 8) & 0xff;
448 *e++ = clientarch & 0xff;
450 *e++ = 94; /* Client Network Interface Identifier */
452 *e++ = 1; /* type field for UNDI */
453 *e++ = 0; /* major revision */
454 *e++ = 0; /* minor revision */
456 uuid = getenv("pxeuuid");
459 if (uuid_str_valid(uuid)) {
460 *e++ = 97; /* Client Machine Identifier */
462 *e++ = 0; /* type 0 - UUID */
464 uuid_str_to_bin(uuid, e);
467 printf("Invalid pxeuuid: %s\n", uuid);
471 *e++ = 60; /* Vendor Class Identifier */
472 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
474 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
478 #if defined(CONFIG_BOOTP_VENDOREX)
479 if ((x = dhcp_vendorex_prep (e)))
483 *e++ = 55; /* Parameter Request List */
484 cnt = e++; /* Pointer to count of requested items */
486 #if defined(CONFIG_BOOTP_SUBNETMASK)
487 *e++ = 1; /* Subnet Mask */
490 #if defined(CONFIG_BOOTP_TIMEOFFSET)
494 #if defined(CONFIG_BOOTP_GATEWAY)
495 *e++ = 3; /* Router Option */
498 #if defined(CONFIG_BOOTP_DNS)
499 *e++ = 6; /* DNS Server(s) */
502 #if defined(CONFIG_BOOTP_HOSTNAME)
503 *e++ = 12; /* Hostname */
506 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
507 *e++ = 13; /* Boot File Size */
510 #if defined(CONFIG_BOOTP_BOOTPATH)
511 *e++ = 17; /* Boot path */
514 #if defined(CONFIG_BOOTP_NISDOMAIN)
515 *e++ = 40; /* NIS Domain name request */
518 #if defined(CONFIG_BOOTP_NTPSERVER)
522 /* no options, so back up to avoid sending an empty request list */
526 *e++ = 255; /* End of the list */
528 /* Pad to minimal length */
529 #ifdef CONFIG_DHCP_MIN_EXT_LEN
530 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
539 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
541 static int BootpExtended (u8 * e)
545 *e++ = 99; /* RFC1048 Magic Cookie */
550 #if defined(CONFIG_CMD_DHCP)
551 *e++ = 53; /* DHCP Message Type */
553 *e++ = DHCP_DISCOVER;
555 *e++ = 57; /* Maximum DHCP Message Size */
557 *e++ = (576 - 312 + OPT_SIZE) >> 16;
558 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
561 #if defined(CONFIG_BOOTP_SUBNETMASK)
562 *e++ = 1; /* Subnet mask request */
567 #if defined(CONFIG_BOOTP_GATEWAY)
568 *e++ = 3; /* Default gateway request */
573 #if defined(CONFIG_BOOTP_DNS)
574 *e++ = 6; /* Domain Name Server */
579 #if defined(CONFIG_BOOTP_HOSTNAME)
580 *e++ = 12; /* Host name request */
585 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
586 *e++ = 13; /* Boot file size */
591 #if defined(CONFIG_BOOTP_BOOTPATH)
592 *e++ = 17; /* Boot path */
597 #if defined(CONFIG_BOOTP_NISDOMAIN)
598 *e++ = 40; /* NIS Domain name request */
602 #if defined(CONFIG_BOOTP_NTPSERVER)
608 *e++ = 255; /* End of the list */
617 volatile uchar *pkt, *iphdr;
619 int ext_len, pktlen, iplen;
621 #if defined(CONFIG_CMD_DHCP)
625 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
626 unsigned char bi_enetaddr[6];
628 ulong tst1, tst2, sum, m_mask, m_value = 0;
632 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
634 debug("BootpRequest => Our Mac: ");
635 for (reg=0; reg<6; reg++)
636 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
638 /* Mac-Manipulation 2 get seed1 */
641 for (reg=2; reg<6; reg++) {
643 tst1 = tst1 | bi_enetaddr[reg];
645 for (reg=0; reg<2; reg++) {
646 tst2 = tst2 | bi_enetaddr[reg];
654 for (reg=1;reg<=32;reg++) {
655 m_value |= (m_mask & seed1);
657 m_value = m_value << 1;
663 /* Random Number Generator */
665 for (reg=0;reg<=0;reg++) {
667 if (sum < seed1 || sum < seed2)
672 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
673 sum = sum >> (22-BootpTry);
674 } else { /*After 3rd BOOTP request max 8192 * 1ms */
679 printf ("Random delay: %ld ms...\n", sum);
680 for (reg=0; reg <sum; reg++) {
681 udelay(1000); /*Wait 1ms*/
683 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
685 printf("BOOTP broadcast %d\n", ++BootpTry);
687 memset ((void*)pkt, 0, PKTSIZE);
689 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
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.
697 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
698 iphdr = pkt; /* We need this later for NetSetIP() */
702 bp->bp_op = OP_BOOTREQUEST;
703 bp->bp_htype = HWT_ETHER;
704 bp->bp_hlen = HWL_ETHER;
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));
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);
718 ext_len = BootpExtended((u8 *)bp->bp_vend);
722 * Bootp ID is the lower 4 bytes of our ethernet address
723 * plus the current time in ms.
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);
734 * Calculate proper packet lengths taking into account the
735 * variable size of the options field
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);
742 #if defined(CONFIG_CMD_DHCP)
743 dhcp_state = SELECTING;
744 NetSetHandler(DhcpHandler);
746 NetSetHandler(BootpHandler);
748 NetSendPacket(NetTxPacket, pktlen);
751 #if defined(CONFIG_CMD_DHCP)
752 static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
754 uchar *end = popt + BOOTP_HDR_SIZE;
756 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
760 while (popt < end && *popt != 0xff) {
764 NetCopyIP (&NetOurSubnetMask, (popt + 2));
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);
774 NetCopyIP (&NetOurGatewayIP, (popt + 2));
777 NetCopyIP (&NetOurDNSIP, (popt + 2));
778 #if defined(CONFIG_BOOTP_DNS2)
779 if (*(popt + 1) > 4) {
780 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
785 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
786 memcpy (&NetOurHostName, popt + 2, size);
787 NetOurHostName[size] = 0;
789 case 15: /* Ignore Domain Name Option */
792 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
793 memcpy (&NetOurRootPath, popt + 2, size);
794 NetOurRootPath[size] = 0;
796 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
797 case 42: /* NTP server IP */
798 NetCopyIP (&NetNtpServerIP, (popt + 2));
802 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
804 case 53: /* Ignore Message Type Option */
807 NetCopyIP (&NetDHCPServerIP, (popt + 2));
809 case 58: /* Ignore Renewal Time Option */
811 case 59: /* Ignore Rebinding Time Option */
813 case 66: /* Ignore TFTP server name */
815 case 67: /* vendor opt bootfile */
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
822 size = truncate_sz ("Opt Boot File",
825 if (bp->bp_file[0] == '\0' && size > 0) {
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
835 printf("*** WARNING: using vendor "
836 "optional boot file\n");
837 memcpy(bp->bp_file, popt + 2, size);
838 bp->bp_file[size] = '\0';
842 #if defined(CONFIG_BOOTP_VENDOREX)
843 if (dhcp_vendorex_proc (popt))
846 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
849 popt += oplen + 2; /* Process next option */
853 static int DhcpMessageType(unsigned char *popt)
855 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
859 while ( *popt != 0xff ) {
860 if ( *popt == 53 ) /* DHCP Message Type */
862 popt += *(popt + 1) + 2; /* Scan through all options */
867 static void DhcpSendRequestPkt(Bootp_t *bp_offer)
869 volatile uchar *pkt, *iphdr;
871 int pktlen, iplen, extlen;
874 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
876 memset ((void*)pkt, 0, PKTSIZE);
878 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
880 iphdr = pkt; /* We'll need this later to set proper pkt size */
884 bp->bp_op = OP_BOOTREQUEST;
885 bp->bp_htype = HWT_ETHER;
886 bp->bp_hlen = HWL_ETHER;
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
893 * RFC3046 requires Relay Agents to discard packets with
894 * nonzero and offered giaddr
896 NetWriteIP(&bp->bp_giaddr, 0);
898 memcpy (bp->bp_chaddr, NetOurEther, 6);
901 * ID is the id of the OFFER packet
904 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
907 * Copy options from OFFER packet if present
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);
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);
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);
926 * Handle DHCP received packets.
929 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
932 Bootp_t *bp = (Bootp_t *)pkt;
934 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
935 src, dest, len, dhcp_state);
937 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
940 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
941 src, dest, len, dhcp_state);
943 switch (dhcp_state) {
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.
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 */
958 debug("TRANSITIONING TO REQUESTING STATE\n");
959 dhcp_state = REQUESTING;
961 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
962 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
964 NetSetTimeout(TIMEOUT, BootpTimeout);
965 DhcpSendRequestPkt(bp);
966 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
968 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
973 debug("DHCP State: REQUESTING\n");
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 */
980 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
987 /* DHCP client bound to address */
990 puts ("DHCP: INVALID STATE\n");
996 void DhcpRequest(void)
1000 #endif /* CONFIG_CMD_DHCP */