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>
20 #include <linux/compiler.h>
22 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
24 #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
25 #ifndef CONFIG_NET_RETRY_COUNT
26 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
28 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
31 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
32 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
34 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
35 #define CONFIG_DHCP_MIN_EXT_LEN 64
40 #ifdef CONFIG_BOOTP_RANDOM_DELAY
44 #if defined(CONFIG_CMD_DHCP)
45 dhcp_state_t dhcp_state = INIT;
46 unsigned long dhcp_leasetime = 0;
47 IPaddr_t NetDHCPServerIP = 0;
48 static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
53 static char *dhcpmsg2str(int type)
56 case 1: return "DHCPDISCOVER"; break;
57 case 2: return "DHCPOFFER"; break;
58 case 3: return "DHCPREQUEST"; break;
59 case 4: return "DHCPDECLINE"; break;
60 case 5: return "DHCPACK"; break;
61 case 6: return "DHCPNACK"; break;
62 case 7: return "DHCPRELEASE"; break;
63 default: return "UNKNOWN/INVALID MSG TYPE"; break;
68 #if defined(CONFIG_BOOTP_VENDOREX)
69 extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
70 extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
75 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
77 Bootp_t *bp = (Bootp_t *) pkt;
80 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
82 else if (len < sizeof (Bootp_t) - OPT_SIZE)
84 else if (bp->bp_op != OP_BOOTREQUEST &&
85 bp->bp_op != OP_BOOTREPLY &&
86 bp->bp_op != DHCP_OFFER &&
87 bp->bp_op != DHCP_ACK &&
88 bp->bp_op != DHCP_NAK ) {
91 else if (bp->bp_htype != HWT_ETHER)
93 else if (bp->bp_hlen != HWL_ETHER)
95 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
99 debug("Filtering pkt = %d\n", retval);
105 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
107 static void BootpCopyNetParams(Bootp_t *bp)
109 __maybe_unused IPaddr_t tmp_ip;
111 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
112 #if !defined(CONFIG_BOOTP_SERVERIP)
113 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
115 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
116 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
118 if (strlen(bp->bp_file) > 0)
119 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
121 debug("Bootfile: %s\n", BootFile);
123 /* Propagate to environment:
124 * don't delete exising entry when BOOTP / DHCP reply does
125 * not contain a new value
128 setenv ("bootfile", BootFile);
132 static int truncate_sz (const char *name, int maxlen, int curlen)
134 if (curlen >= maxlen) {
135 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
136 name, curlen, maxlen);
142 #if !defined(CONFIG_CMD_DHCP)
144 static void BootpVendorFieldProcess (u8 * ext)
146 int size = *(ext + 1);
148 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
154 /* Fixed length fields */
155 case 1: /* Subnet mask */
156 if (NetOurSubnetMask == 0)
157 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
159 case 2: /* Time offset - Not yet supported */
161 /* Variable length fields */
162 case 3: /* Gateways list */
163 if (NetOurGatewayIP == 0) {
164 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
167 case 4: /* Time server - Not yet supported */
169 case 5: /* IEN-116 name server - Not yet supported */
172 if (NetOurDNSIP == 0) {
173 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
175 #if defined(CONFIG_BOOTP_DNS2)
176 if ((NetOurDNS2IP == 0) && (size > 4)) {
177 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
181 case 7: /* Log server - Not yet supported */
183 case 8: /* Cookie/Quote server - Not yet supported */
185 case 9: /* LPR server - Not yet supported */
187 case 10: /* Impress server - Not yet supported */
189 case 11: /* RPL server - Not yet supported */
191 case 12: /* Host name */
192 if (NetOurHostName[0] == 0) {
193 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
194 memcpy (&NetOurHostName, ext + 2, size);
195 NetOurHostName[size] = 0;
198 case 13: /* Boot file size */
200 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
202 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
204 case 14: /* Merit dump file - Not yet supported */
206 case 15: /* Domain name - Not yet supported */
208 case 16: /* Swap server - Not yet supported */
210 case 17: /* Root path */
211 if (NetOurRootPath[0] == 0) {
212 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
213 memcpy (&NetOurRootPath, ext + 2, size);
214 NetOurRootPath[size] = 0;
217 case 18: /* Extension path - Not yet supported */
219 * This can be used to send the information of the
220 * vendor area in another file that the client can
224 /* IP host layer fields */
225 case 40: /* NIS Domain name */
226 if (NetOurNISDomain[0] == 0) {
227 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
228 memcpy (&NetOurNISDomain, ext + 2, size);
229 NetOurNISDomain[size] = 0;
232 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
233 case 42: /* NTP server IP */
234 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
237 /* Application layer fields */
238 case 43: /* Vendor specific info - Not yet supported */
240 * Binary information to exchange specific
241 * product information.
244 /* Reserved (custom) fields (128..254) */
248 static void BootpVendorProcess (u8 * ext, int size)
250 u8 *end = ext + size;
252 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
254 while ((ext < end) && (*ext != 0xff)) {
262 BootpVendorFieldProcess (opt);
266 debug("[BOOTP] Received fields: \n");
267 if (NetOurSubnetMask)
268 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
271 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
274 debug("NetBootFileSize : %d\n", NetBootFileSize);
276 if (NetOurHostName[0])
277 debug("NetOurHostName : %s\n", NetOurHostName);
279 if (NetOurRootPath[0])
280 debug("NetOurRootPath : %s\n", NetOurRootPath);
282 if (NetOurNISDomain[0])
283 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
286 debug("NetBootFileSize: %d\n", NetBootFileSize);
288 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
290 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
295 * Handle a BOOTP received packet.
298 BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
303 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
304 src, dest, len, sizeof (Bootp_t));
308 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
312 * Got a good BOOTP reply. Copy the data into our variables.
314 #ifdef CONFIG_STATUS_LED
315 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
318 BootpCopyNetParams(bp); /* Store net parameters from reply */
320 /* Retrieve extended information (we must parse the vendor area) */
321 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
322 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
324 NetSetTimeout(0, (thand_f *)0);
325 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
327 debug("Got good BOOTP\n");
334 * Timeout on BOOTP/DHCP request.
339 if (BootpTry >= TIMEOUT_COUNT) {
340 puts ("\nRetry count exceeded; starting again\n");
343 NetSetTimeout (TIMEOUT, BootpTimeout);
349 * Initialize BOOTP extension fields in the request.
351 #if defined(CONFIG_CMD_DHCP)
352 static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
356 #if defined(CONFIG_BOOTP_PXE)
362 #if defined(CONFIG_BOOTP_VENDOREX)
365 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
369 *e++ = 99; /* RFC1048 Magic Cookie */
374 *e++ = 53; /* DHCP Message Type */
378 *e++ = 57; /* Maximum DHCP Message Size */
380 *e++ = (576 - 312 + OPT_SIZE) >> 8;
381 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
384 int tmp = ntohl (ServerID);
386 *e++ = 54; /* ServerID */
395 int tmp = ntohl (RequestedIP);
397 *e++ = 50; /* Requested IP */
404 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
405 if ((hostname = getenv ("hostname"))) {
406 int hostnamelen = strlen (hostname);
408 *e++ = 12; /* Hostname */
410 memcpy (e, hostname, hostnamelen);
415 #if defined(CONFIG_BOOTP_PXE)
416 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
417 *e++ = 93; /* Client System Architecture */
419 *e++ = (clientarch >> 8) & 0xff;
420 *e++ = clientarch & 0xff;
422 *e++ = 94; /* Client Network Interface Identifier */
424 *e++ = 1; /* type field for UNDI */
425 *e++ = 0; /* major revision */
426 *e++ = 0; /* minor revision */
428 uuid = getenv("pxeuuid");
431 if (uuid_str_valid(uuid)) {
432 *e++ = 97; /* Client Machine Identifier */
434 *e++ = 0; /* type 0 - UUID */
436 uuid_str_to_bin(uuid, e);
439 printf("Invalid pxeuuid: %s\n", uuid);
443 *e++ = 60; /* Vendor Class Identifier */
444 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
446 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
450 #if defined(CONFIG_BOOTP_VENDOREX)
451 if ((x = dhcp_vendorex_prep (e)))
455 *e++ = 55; /* Parameter Request List */
456 cnt = e++; /* Pointer to count of requested items */
458 #if defined(CONFIG_BOOTP_SUBNETMASK)
459 *e++ = 1; /* Subnet Mask */
462 #if defined(CONFIG_BOOTP_TIMEOFFSET)
466 #if defined(CONFIG_BOOTP_GATEWAY)
467 *e++ = 3; /* Router Option */
470 #if defined(CONFIG_BOOTP_DNS)
471 *e++ = 6; /* DNS Server(s) */
474 #if defined(CONFIG_BOOTP_HOSTNAME)
475 *e++ = 12; /* Hostname */
478 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
479 *e++ = 13; /* Boot File Size */
482 #if defined(CONFIG_BOOTP_BOOTPATH)
483 *e++ = 17; /* Boot path */
486 #if defined(CONFIG_BOOTP_NISDOMAIN)
487 *e++ = 40; /* NIS Domain name request */
490 #if defined(CONFIG_BOOTP_NTPSERVER)
494 /* no options, so back up to avoid sending an empty request list */
498 *e++ = 255; /* End of the list */
500 /* Pad to minimal length */
501 #ifdef CONFIG_DHCP_MIN_EXT_LEN
502 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
511 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
513 static int BootpExtended (u8 * e)
517 *e++ = 99; /* RFC1048 Magic Cookie */
522 #if defined(CONFIG_CMD_DHCP)
523 *e++ = 53; /* DHCP Message Type */
525 *e++ = DHCP_DISCOVER;
527 *e++ = 57; /* Maximum DHCP Message Size */
529 *e++ = (576 - 312 + OPT_SIZE) >> 16;
530 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
533 #if defined(CONFIG_BOOTP_SUBNETMASK)
534 *e++ = 1; /* Subnet mask request */
539 #if defined(CONFIG_BOOTP_GATEWAY)
540 *e++ = 3; /* Default gateway request */
545 #if defined(CONFIG_BOOTP_DNS)
546 *e++ = 6; /* Domain Name Server */
551 #if defined(CONFIG_BOOTP_HOSTNAME)
552 *e++ = 12; /* Host name request */
557 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
558 *e++ = 13; /* Boot file size */
563 #if defined(CONFIG_BOOTP_BOOTPATH)
564 *e++ = 17; /* Boot path */
569 #if defined(CONFIG_BOOTP_NISDOMAIN)
570 *e++ = 40; /* NIS Domain name request */
574 #if defined(CONFIG_BOOTP_NTPSERVER)
580 *e++ = 255; /* End of the list */
589 volatile uchar *pkt, *iphdr;
591 int ext_len, pktlen, iplen;
593 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
594 #if defined(CONFIG_CMD_DHCP)
598 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
599 unsigned char bi_enetaddr[6];
601 ulong tst1, tst2, sum, m_mask, m_value = 0;
605 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
607 debug("BootpRequest => Our Mac: ");
608 for (reg=0; reg<6; reg++)
609 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
611 /* Mac-Manipulation 2 get seed1 */
614 for (reg=2; reg<6; reg++) {
616 tst1 = tst1 | bi_enetaddr[reg];
618 for (reg=0; reg<2; reg++) {
619 tst2 = tst2 | bi_enetaddr[reg];
627 for (reg=1;reg<=32;reg++) {
628 m_value |= (m_mask & seed1);
630 m_value = m_value << 1;
636 /* Random Number Generator */
638 for (reg=0;reg<=0;reg++) {
640 if (sum < seed1 || sum < seed2)
645 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
646 sum = sum >> (22-BootpTry);
647 } else { /*After 3rd BOOTP request max 8192 * 1ms */
652 printf ("Random delay: %ld ms...\n", sum);
653 for (reg=0; reg <sum; reg++) {
654 udelay(1000); /*Wait 1ms*/
656 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
658 printf("BOOTP broadcast %d\n", ++BootpTry);
660 memset ((void*)pkt, 0, PKTSIZE);
662 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
665 * Next line results in incorrect packet size being transmitted, resulting
666 * in errors in some DHCP servers, reporting missing bytes. Size must be
667 * set in packet header after extension length has been determined.
668 * C. Hallinan, DS4.COM, Inc.
670 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
671 iphdr = pkt; /* We need this later for NetSetIP() */
675 bp->bp_op = OP_BOOTREQUEST;
676 bp->bp_htype = HWT_ETHER;
677 bp->bp_hlen = HWL_ETHER;
679 bp->bp_secs = htons(get_timer(0) / 1000);
680 NetWriteIP(&bp->bp_ciaddr, 0);
681 NetWriteIP(&bp->bp_yiaddr, 0);
682 NetWriteIP(&bp->bp_siaddr, 0);
683 NetWriteIP(&bp->bp_giaddr, 0);
684 memcpy (bp->bp_chaddr, NetOurEther, 6);
685 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
687 /* Request additional information from the BOOTP/DHCP server */
688 #if defined(CONFIG_CMD_DHCP)
689 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
691 ext_len = BootpExtended((u8 *)bp->bp_vend);
695 * Bootp ID is the lower 4 bytes of our ethernet address
696 * plus the current time in ms.
698 BootpID = ((ulong)NetOurEther[2] << 24)
699 | ((ulong)NetOurEther[3] << 16)
700 | ((ulong)NetOurEther[4] << 8)
701 | (ulong)NetOurEther[5];
702 BootpID += get_timer(0);
703 BootpID = htonl(BootpID);
704 NetCopyLong(&bp->bp_id, &BootpID);
707 * Calculate proper packet lengths taking into account the
708 * variable size of the options field
710 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
711 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
712 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
713 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
715 #if defined(CONFIG_CMD_DHCP)
716 dhcp_state = SELECTING;
717 NetSetHandler(DhcpHandler);
719 NetSetHandler(BootpHandler);
721 NetSendPacket(NetTxPacket, pktlen);
724 #if defined(CONFIG_CMD_DHCP)
725 static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
727 uchar *end = popt + BOOTP_HDR_SIZE;
729 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
733 while (popt < end && *popt != 0xff) {
737 NetCopyIP (&NetOurSubnetMask, (popt + 2));
739 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
740 case 2: /* Time offset */
741 to_ptr = &NetTimeOffset;
742 NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
743 NetTimeOffset = ntohl (NetTimeOffset);
747 NetCopyIP (&NetOurGatewayIP, (popt + 2));
750 NetCopyIP (&NetOurDNSIP, (popt + 2));
751 #if defined(CONFIG_BOOTP_DNS2)
752 if (*(popt + 1) > 4) {
753 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
758 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
759 memcpy (&NetOurHostName, popt + 2, size);
760 NetOurHostName[size] = 0;
762 case 15: /* Ignore Domain Name Option */
765 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
766 memcpy (&NetOurRootPath, popt + 2, size);
767 NetOurRootPath[size] = 0;
769 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
770 case 42: /* NTP server IP */
771 NetCopyIP (&NetNtpServerIP, (popt + 2));
775 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
777 case 53: /* Ignore Message Type Option */
780 NetCopyIP (&NetDHCPServerIP, (popt + 2));
782 case 58: /* Ignore Renewal Time Option */
784 case 59: /* Ignore Rebinding Time Option */
786 case 66: /* Ignore TFTP server name */
788 case 67: /* vendor opt bootfile */
790 * I can't use dhcp_vendorex_proc here because I need
791 * to write into the bootp packet - even then I had to
792 * pass the bootp packet pointer into here as the
795 size = truncate_sz ("Opt Boot File",
798 if (bp->bp_file[0] == '\0' && size > 0) {
800 * only use vendor boot file if we didn't
801 * receive a boot file in the main non-vendor
802 * part of the packet - god only knows why
803 * some vendors chose not to use this perfectly
804 * good spot to store the boot file (join on
805 * Tru64 Unix) it seems mind bogglingly crazy
808 printf("*** WARNING: using vendor "
809 "optional boot file\n");
810 memcpy(bp->bp_file, popt + 2, size);
811 bp->bp_file[size] = '\0';
815 #if defined(CONFIG_BOOTP_VENDOREX)
816 if (dhcp_vendorex_proc (popt))
819 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
822 popt += oplen + 2; /* Process next option */
826 static int DhcpMessageType(unsigned char *popt)
828 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
832 while ( *popt != 0xff ) {
833 if ( *popt == 53 ) /* DHCP Message Type */
835 popt += *(popt + 1) + 2; /* Scan through all options */
840 static void DhcpSendRequestPkt(Bootp_t *bp_offer)
842 volatile uchar *pkt, *iphdr;
844 int pktlen, iplen, extlen;
847 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
849 memset ((void*)pkt, 0, PKTSIZE);
851 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
853 iphdr = pkt; /* We'll need this later to set proper pkt size */
857 bp->bp_op = OP_BOOTREQUEST;
858 bp->bp_htype = HWT_ETHER;
859 bp->bp_hlen = HWL_ETHER;
861 bp->bp_secs = htons(get_timer(0) / 1000);
862 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
866 * RFC3046 requires Relay Agents to discard packets with
867 * nonzero and offered giaddr
869 NetWriteIP(&bp->bp_giaddr, 0);
871 memcpy (bp->bp_chaddr, NetOurEther, 6);
874 * ID is the id of the OFFER packet
877 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
880 * Copy options from OFFER packet if present
883 /* Copy offered IP into the parameters request list */
884 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
885 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
887 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
888 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
889 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
891 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
892 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
893 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
894 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
895 NetSendPacket(NetTxPacket, pktlen);
899 * Handle DHCP received packets.
902 DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
905 Bootp_t *bp = (Bootp_t *)pkt;
907 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
908 src, dest, len, dhcp_state);
910 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
913 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
914 src, dest, len, dhcp_state);
916 switch (dhcp_state) {
919 * Wait an appropriate time for any potential DHCPOFFER packets
920 * to arrive. Then select one, and generate DHCPREQUEST response.
921 * If filename is in format we recognize, assume it is a valid
922 * OFFER from a server we want.
924 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
925 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
926 if (strncmp(bp->bp_file,
927 CONFIG_SYS_BOOTFILE_PREFIX,
928 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
929 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
931 debug("TRANSITIONING TO REQUESTING STATE\n");
932 dhcp_state = REQUESTING;
934 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
935 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
937 NetSetTimeout(TIMEOUT, BootpTimeout);
938 DhcpSendRequestPkt(bp);
939 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
941 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
946 debug("DHCP State: REQUESTING\n");
948 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
949 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
950 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
951 BootpCopyNetParams(bp); /* Store net params from reply */
953 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
954 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
962 /* DHCP client bound to address */
965 puts ("DHCP: INVALID STATE\n");
971 void DhcpRequest(void)
975 #endif /* CONFIG_CMD_DHCP */