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
18 #ifdef CONFIG_STATUS_LED
19 #include <status_led.h>
21 #include <linux/compiler.h>
23 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
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 */
29 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
32 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
33 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
35 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
36 #define CONFIG_DHCP_MIN_EXT_LEN 64
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
,
51 static char *dhcpmsg2str(int 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;
67 static int BootpCheckPkt(uchar
*pkt
, unsigned dest
, unsigned src
, unsigned len
)
69 struct Bootp_t
*bp
= (struct Bootp_t
*) pkt
;
72 if (dest
!= PORT_BOOTPC
|| src
!= PORT_BOOTPS
)
74 else if (len
< sizeof(struct Bootp_t
) - OPT_FIELD_SIZE
)
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
)
82 else if (bp
->bp_htype
!= HWT_ETHER
)
84 else if (bp
->bp_hlen
!= HWL_ETHER
)
86 else if (NetReadLong((ulong
*)&bp
->bp_id
) != BootpID
)
89 debug("Filtering pkt = %d\n", retval
);
95 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
97 static void BootpCopyNetParams(struct Bootp_t
*bp
)
99 __maybe_unused IPaddr_t tmp_ip
;
101 NetCopyIP(&NetOurIP
, &bp
->bp_yiaddr
);
102 #if !defined(CONFIG_BOOTP_SERVERIP)
103 NetCopyIP(&tmp_ip
, &bp
->bp_siaddr
);
105 NetCopyIP(&NetServerIP
, &bp
->bp_siaddr
);
106 memcpy(NetServerEther
, ((struct ethernet_hdr
*)NetRxPacket
)->et_src
, 6);
108 if (strlen(bp
->bp_file
) > 0)
109 copy_filename(BootFile
, bp
->bp_file
, sizeof(BootFile
));
111 debug("Bootfile: %s\n", BootFile
);
113 /* Propagate to environment:
114 * don't delete exising entry when BOOTP / DHCP reply does
115 * not contain a new value
118 setenv("bootfile", BootFile
);
121 static int truncate_sz(const char *name
, int maxlen
, int curlen
)
123 if (curlen
>= maxlen
) {
124 printf("*** WARNING: %s is too long (%d - max: %d)"
125 " - truncated\n", name
, curlen
, maxlen
);
131 #if !defined(CONFIG_CMD_DHCP)
133 static void BootpVendorFieldProcess(u8
*ext
)
135 int size
= *(ext
+ 1);
137 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext
,
143 /* Fixed length fields */
144 case 1: /* Subnet mask */
145 if (NetOurSubnetMask
== 0)
146 NetCopyIP(&NetOurSubnetMask
, (IPaddr_t
*) (ext
+ 2));
148 case 2: /* Time offset - Not yet supported */
150 /* Variable length fields */
151 case 3: /* Gateways list */
152 if (NetOurGatewayIP
== 0)
153 NetCopyIP(&NetOurGatewayIP
, (IPaddr_t
*) (ext
+ 2));
155 case 4: /* Time server - Not yet supported */
157 case 5: /* IEN-116 name server - Not yet supported */
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));
167 case 7: /* Log server - Not yet supported */
169 case 8: /* Cookie/Quote server - Not yet supported */
171 case 9: /* LPR server - Not yet supported */
173 case 10: /* Impress server - Not yet supported */
175 case 11: /* RPL server - Not yet supported */
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;
185 case 13: /* Boot file size */
187 NetBootFileSize
= ntohs(*(ushort
*) (ext
+ 2));
189 NetBootFileSize
= ntohl(*(ulong
*) (ext
+ 2));
191 case 14: /* Merit dump file - Not yet supported */
193 case 15: /* Domain name - Not yet supported */
195 case 16: /* Swap server - Not yet supported */
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;
205 case 18: /* Extension path - Not yet supported */
207 * This can be used to send the information of the
208 * vendor area in another file that the client can
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;
221 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
222 case 42: /* NTP server IP */
223 NetCopyIP(&NetNtpServerIP
, (IPaddr_t
*) (ext
+ 2));
226 /* Application layer fields */
227 case 43: /* Vendor specific info - Not yet supported */
229 * Binary information to exchange specific
230 * product information.
233 /* Reserved (custom) fields (128..254) */
237 static void BootpVendorProcess(u8
*ext
, int size
)
239 u8
*end
= ext
+ size
;
241 debug("[BOOTP] Checking extension (%d bytes)...\n", size
);
243 while ((ext
< end
) && (*ext
!= 0xff)) {
251 BootpVendorFieldProcess(opt
);
255 debug("[BOOTP] Received fields:\n");
256 if (NetOurSubnetMask
)
257 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask
);
260 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP
);
263 debug("NetBootFileSize : %d\n", NetBootFileSize
);
265 if (NetOurHostName
[0])
266 debug("NetOurHostName : %s\n", NetOurHostName
);
268 if (NetOurRootPath
[0])
269 debug("NetOurRootPath : %s\n", NetOurRootPath
);
271 if (NetOurNISDomain
[0])
272 debug("NetOurNISDomain : %s\n", NetOurNISDomain
);
275 debug("NetBootFileSize: %d\n", NetBootFileSize
);
277 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
279 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP
);
284 * Handle a BOOTP received packet.
287 BootpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
292 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
293 src
, dest
, len
, sizeof(struct Bootp_t
));
295 bp
= (struct Bootp_t
*)pkt
;
297 /* Filter out pkts we don't want */
298 if (BootpCheckPkt(pkt
, dest
, src
, len
))
302 * Got a good BOOTP reply. Copy the data into our variables.
304 #ifdef CONFIG_STATUS_LED
305 status_led_set(STATUS_LED_BOOT
, STATUS_LED_OFF
);
308 BootpCopyNetParams(bp
); /* Store net parameters from reply */
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
);
314 NetSetTimeout(0, (thand_f
*)0);
315 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP
, "bootp_stop");
317 debug("Got good BOOTP\n");
324 * Timeout on BOOTP/DHCP request.
329 if (BootpTry
>= TIMEOUT_COUNT
) {
330 puts("\nRetry count exceeded; starting again\n");
333 NetSetTimeout(TIMEOUT
, BootpTimeout
);
339 * Initialize BOOTP extension fields in the request.
341 #if defined(CONFIG_CMD_DHCP)
342 static int DhcpExtended(u8
*e
, int message_type
, IPaddr_t ServerID
,
343 IPaddr_t RequestedIP
)
347 #if defined(CONFIG_BOOTP_PXE)
353 #if defined(CONFIG_BOOTP_VENDOREX)
356 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
360 *e
++ = 99; /* RFC1048 Magic Cookie */
365 *e
++ = 53; /* DHCP Message Type */
369 *e
++ = 57; /* Maximum DHCP Message Size */
371 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) >> 8;
372 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) & 0xff;
375 int tmp
= ntohl(ServerID
);
377 *e
++ = 54; /* ServerID */
386 int tmp
= ntohl(RequestedIP
);
388 *e
++ = 50; /* Requested IP */
395 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
396 hostname
= getenv("hostname");
398 int hostnamelen
= strlen(hostname
);
400 *e
++ = 12; /* Hostname */
402 memcpy(e
, hostname
, hostnamelen
);
407 #if defined(CONFIG_BOOTP_PXE)
408 clientarch
= CONFIG_BOOTP_PXE_CLIENTARCH
;
409 *e
++ = 93; /* Client System Architecture */
411 *e
++ = (clientarch
>> 8) & 0xff;
412 *e
++ = clientarch
& 0xff;
414 *e
++ = 94; /* Client Network Interface Identifier */
416 *e
++ = 1; /* type field for UNDI */
417 *e
++ = 0; /* major revision */
418 *e
++ = 0; /* minor revision */
420 uuid
= getenv("pxeuuid");
423 if (uuid_str_valid(uuid
)) {
424 *e
++ = 97; /* Client Machine Identifier */
426 *e
++ = 0; /* type 0 - UUID */
428 uuid_str_to_bin(uuid
, e
);
431 printf("Invalid pxeuuid: %s\n", uuid
);
435 *e
++ = 60; /* Vendor Class Identifier */
436 vci_strlen
= strlen(CONFIG_BOOTP_VCI_STRING
);
438 memcpy(e
, CONFIG_BOOTP_VCI_STRING
, vci_strlen
);
442 #if defined(CONFIG_BOOTP_VENDOREX)
443 x
= dhcp_vendorex_prep(e
);
448 *e
++ = 55; /* Parameter Request List */
449 cnt
= e
++; /* Pointer to count of requested items */
451 #if defined(CONFIG_BOOTP_SUBNETMASK)
452 *e
++ = 1; /* Subnet Mask */
455 #if defined(CONFIG_BOOTP_TIMEOFFSET)
459 #if defined(CONFIG_BOOTP_GATEWAY)
460 *e
++ = 3; /* Router Option */
463 #if defined(CONFIG_BOOTP_DNS)
464 *e
++ = 6; /* DNS Server(s) */
467 #if defined(CONFIG_BOOTP_HOSTNAME)
468 *e
++ = 12; /* Hostname */
471 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
472 *e
++ = 13; /* Boot File Size */
475 #if defined(CONFIG_BOOTP_BOOTPATH)
476 *e
++ = 17; /* Boot path */
479 #if defined(CONFIG_BOOTP_NISDOMAIN)
480 *e
++ = 40; /* NIS Domain name request */
483 #if defined(CONFIG_BOOTP_NTPSERVER)
487 /* no options, so back up to avoid sending an empty request list */
491 *e
++ = 255; /* End of the list */
493 /* Pad to minimal length */
494 #ifdef CONFIG_DHCP_MIN_EXT_LEN
495 while ((e
- start
) < CONFIG_DHCP_MIN_EXT_LEN
)
504 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
506 static int BootpExtended(u8
*e
)
510 *e
++ = 99; /* RFC1048 Magic Cookie */
515 #if defined(CONFIG_CMD_DHCP)
516 *e
++ = 53; /* DHCP Message Type */
518 *e
++ = DHCP_DISCOVER
;
520 *e
++ = 57; /* Maximum DHCP Message Size */
522 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) >> 16;
523 *e
++ = (576 - 312 + OPT_FIELD_SIZE
) & 0xff;
526 #if defined(CONFIG_BOOTP_SUBNETMASK)
527 *e
++ = 1; /* Subnet mask request */
532 #if defined(CONFIG_BOOTP_GATEWAY)
533 *e
++ = 3; /* Default gateway request */
538 #if defined(CONFIG_BOOTP_DNS)
539 *e
++ = 6; /* Domain Name Server */
544 #if defined(CONFIG_BOOTP_HOSTNAME)
545 *e
++ = 12; /* Host name request */
550 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
551 *e
++ = 13; /* Boot file size */
556 #if defined(CONFIG_BOOTP_BOOTPATH)
557 *e
++ = 17; /* Boot path */
562 #if defined(CONFIG_BOOTP_NISDOMAIN)
563 *e
++ = 40; /* NIS Domain name request */
567 #if defined(CONFIG_BOOTP_NTPSERVER)
573 *e
++ = 255; /* End of the list */
584 int extlen
, pktlen
, iplen
;
586 #ifdef CONFIG_BOOTP_RANDOM_DELAY
590 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START
, "bootp_start");
591 #if defined(CONFIG_CMD_DHCP)
595 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
599 if (BootpTry
<= 2) /* Start with max 1024 * 1ms */
600 rand_ms
= rand() >> (22 - BootpTry
);
601 else /* After 3rd BOOTP request max 8192 * 1ms */
602 rand_ms
= rand() >> 19;
604 printf("Random delay: %ld ms...\n", rand_ms
);
605 for (i
= 0; i
< rand_ms
; i
++)
606 udelay(1000); /*Wait 1ms*/
608 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
610 printf("BOOTP broadcast %d\n", ++BootpTry
);
612 memset((void *)pkt
, 0, PKTSIZE
);
614 eth_hdr_size
= NetSetEther(pkt
, NetBcastAddr
, PROT_IP
);
618 * Next line results in incorrect packet size being transmitted,
619 * resulting in errors in some DHCP servers, reporting missing bytes.
620 * Size must be set in packet header after extension length has been
622 * C. Hallinan, DS4.COM, Inc.
624 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
625 sizeof (struct Bootp_t)); */
626 iphdr
= pkt
; /* We need this later for net_set_udp_header() */
627 pkt
+= IP_UDP_HDR_SIZE
;
629 bp
= (struct Bootp_t
*)pkt
;
630 bp
->bp_op
= OP_BOOTREQUEST
;
631 bp
->bp_htype
= HWT_ETHER
;
632 bp
->bp_hlen
= HWL_ETHER
;
634 bp
->bp_secs
= htons(get_timer(0) / 1000);
635 NetWriteIP(&bp
->bp_ciaddr
, 0);
636 NetWriteIP(&bp
->bp_yiaddr
, 0);
637 NetWriteIP(&bp
->bp_siaddr
, 0);
638 NetWriteIP(&bp
->bp_giaddr
, 0);
639 memcpy(bp
->bp_chaddr
, NetOurEther
, 6);
640 copy_filename(bp
->bp_file
, BootFile
, sizeof(bp
->bp_file
));
642 /* Request additional information from the BOOTP/DHCP server */
643 #if defined(CONFIG_CMD_DHCP)
644 extlen
= DhcpExtended((u8
*)bp
->bp_vend
, DHCP_DISCOVER
, 0, 0);
646 extlen
= BootpExtended((u8
*)bp
->bp_vend
);
650 * Bootp ID is the lower 4 bytes of our ethernet address
651 * plus the current time in ms.
653 BootpID
= ((ulong
)NetOurEther
[2] << 24)
654 | ((ulong
)NetOurEther
[3] << 16)
655 | ((ulong
)NetOurEther
[4] << 8)
656 | (ulong
)NetOurEther
[5];
657 BootpID
+= get_timer(0);
658 BootpID
= htonl(BootpID
);
659 NetCopyLong(&bp
->bp_id
, &BootpID
);
662 * Calculate proper packet lengths taking into account the
663 * variable size of the options field
665 iplen
= BOOTP_HDR_SIZE
- OPT_FIELD_SIZE
+ extlen
;
666 pktlen
= eth_hdr_size
+ IP_UDP_HDR_SIZE
+ iplen
;
667 net_set_udp_header(iphdr
, 0xFFFFFFFFL
, PORT_BOOTPS
, PORT_BOOTPC
, iplen
);
668 NetSetTimeout(SELECT_TIMEOUT
, BootpTimeout
);
670 #if defined(CONFIG_CMD_DHCP)
671 dhcp_state
= SELECTING
;
672 NetSetHandler(DhcpHandler
);
674 NetSetHandler(BootpHandler
);
676 NetSendPacket(NetTxPacket
, pktlen
);
679 #if defined(CONFIG_CMD_DHCP)
680 static void DhcpOptionsProcess(uchar
*popt
, struct Bootp_t
*bp
)
682 uchar
*end
= popt
+ BOOTP_HDR_SIZE
;
684 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
688 while (popt
< end
&& *popt
!= 0xff) {
692 NetCopyIP(&NetOurSubnetMask
, (popt
+ 2));
694 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
695 case 2: /* Time offset */
696 to_ptr
= &NetTimeOffset
;
697 NetCopyLong((ulong
*)to_ptr
, (ulong
*)(popt
+ 2));
698 NetTimeOffset
= ntohl(NetTimeOffset
);
702 NetCopyIP(&NetOurGatewayIP
, (popt
+ 2));
705 NetCopyIP(&NetOurDNSIP
, (popt
+ 2));
706 #if defined(CONFIG_BOOTP_DNS2)
708 NetCopyIP(&NetOurDNS2IP
, (popt
+ 2 + 4));
712 size
= truncate_sz("Host Name",
713 sizeof(NetOurHostName
), oplen
);
714 memcpy(&NetOurHostName
, popt
+ 2, size
);
715 NetOurHostName
[size
] = 0;
717 case 15: /* Ignore Domain Name Option */
720 size
= truncate_sz("Root Path",
721 sizeof(NetOurRootPath
), oplen
);
722 memcpy(&NetOurRootPath
, popt
+ 2, size
);
723 NetOurRootPath
[size
] = 0;
725 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
726 case 42: /* NTP server IP */
727 NetCopyIP(&NetNtpServerIP
, (popt
+ 2));
731 NetCopyLong(&dhcp_leasetime
, (ulong
*) (popt
+ 2));
733 case 53: /* Ignore Message Type Option */
736 NetCopyIP(&NetDHCPServerIP
, (popt
+ 2));
738 case 58: /* Ignore Renewal Time Option */
740 case 59: /* Ignore Rebinding Time Option */
742 case 66: /* Ignore TFTP server name */
744 case 67: /* vendor opt bootfile */
746 * I can't use dhcp_vendorex_proc here because I need
747 * to write into the bootp packet - even then I had to
748 * pass the bootp packet pointer into here as the
751 size
= truncate_sz("Opt Boot File",
754 if (bp
->bp_file
[0] == '\0' && size
> 0) {
756 * only use vendor boot file if we didn't
757 * receive a boot file in the main non-vendor
758 * part of the packet - god only knows why
759 * some vendors chose not to use this perfectly
760 * good spot to store the boot file (join on
761 * Tru64 Unix) it seems mind bogglingly crazy
764 printf("*** WARNING: using vendor "
765 "optional boot file\n");
766 memcpy(bp
->bp_file
, popt
+ 2, size
);
767 bp
->bp_file
[size
] = '\0';
771 #if defined(CONFIG_BOOTP_VENDOREX)
772 if (dhcp_vendorex_proc(popt
))
775 printf("*** Unhandled DHCP Option in OFFER/ACK:"
779 popt
+= oplen
+ 2; /* Process next option */
783 static int DhcpMessageType(unsigned char *popt
)
785 if (NetReadLong((ulong
*)popt
) != htonl(BOOTP_VENDOR_MAGIC
))
789 while (*popt
!= 0xff) {
790 if (*popt
== 53) /* DHCP Message Type */
792 popt
+= *(popt
+ 1) + 2; /* Scan through all options */
797 static void DhcpSendRequestPkt(struct Bootp_t
*bp_offer
)
801 int pktlen
, iplen
, extlen
;
805 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
807 memset((void *)pkt
, 0, PKTSIZE
);
809 eth_hdr_size
= NetSetEther(pkt
, NetBcastAddr
, PROT_IP
);
812 iphdr
= pkt
; /* We'll need this later to set proper pkt size */
813 pkt
+= IP_UDP_HDR_SIZE
;
815 bp
= (struct Bootp_t
*)pkt
;
816 bp
->bp_op
= OP_BOOTREQUEST
;
817 bp
->bp_htype
= HWT_ETHER
;
818 bp
->bp_hlen
= HWL_ETHER
;
820 bp
->bp_secs
= htons(get_timer(0) / 1000);
821 /* Do not set the client IP, your IP, or server IP yet, since it
822 * hasn't been ACK'ed by the server yet */
825 * RFC3046 requires Relay Agents to discard packets with
826 * nonzero and offered giaddr
828 NetWriteIP(&bp
->bp_giaddr
, 0);
830 memcpy(bp
->bp_chaddr
, NetOurEther
, 6);
833 * ID is the id of the OFFER packet
836 NetCopyLong(&bp
->bp_id
, &bp_offer
->bp_id
);
839 * Copy options from OFFER packet if present
842 /* Copy offered IP into the parameters request list */
843 NetCopyIP(&OfferedIP
, &bp_offer
->bp_yiaddr
);
844 extlen
= DhcpExtended((u8
*)bp
->bp_vend
, DHCP_REQUEST
,
845 NetDHCPServerIP
, OfferedIP
);
847 iplen
= BOOTP_HDR_SIZE
- OPT_FIELD_SIZE
+ extlen
;
848 pktlen
= eth_hdr_size
+ IP_UDP_HDR_SIZE
+ iplen
;
849 net_set_udp_header(iphdr
, 0xFFFFFFFFL
, PORT_BOOTPS
, PORT_BOOTPC
, iplen
);
851 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
852 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY
);
853 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
854 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen
);
855 NetSendPacket(NetTxPacket
, pktlen
);
859 * Handle DHCP received packets.
862 DhcpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
865 struct Bootp_t
*bp
= (struct Bootp_t
*)pkt
;
867 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
868 src
, dest
, len
, dhcp_state
);
870 /* Filter out pkts we don't want */
871 if (BootpCheckPkt(pkt
, dest
, src
, len
))
874 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
875 " %d\n", src
, dest
, len
, dhcp_state
);
877 switch (dhcp_state
) {
880 * Wait an appropriate time for any potential DHCPOFFER packets
881 * to arrive. Then select one, and generate DHCPREQUEST
882 * response. If filename is in format we recognize, assume it
883 * is a valid OFFER from a server we want.
885 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp
->bp_file
);
886 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
887 if (strncmp(bp
->bp_file
,
888 CONFIG_SYS_BOOTFILE_PREFIX
,
889 strlen(CONFIG_SYS_BOOTFILE_PREFIX
)) == 0) {
890 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
892 debug("TRANSITIONING TO REQUESTING STATE\n");
893 dhcp_state
= REQUESTING
;
895 if (NetReadLong((ulong
*)&bp
->bp_vend
[0]) ==
896 htonl(BOOTP_VENDOR_MAGIC
))
897 DhcpOptionsProcess((u8
*)&bp
->bp_vend
[4], bp
);
899 NetSetTimeout(TIMEOUT
, BootpTimeout
);
900 DhcpSendRequestPkt(bp
);
901 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
903 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
908 debug("DHCP State: REQUESTING\n");
910 if (DhcpMessageType((u8
*)bp
->bp_vend
) == DHCP_ACK
) {
911 if (NetReadLong((ulong
*)&bp
->bp_vend
[0]) ==
912 htonl(BOOTP_VENDOR_MAGIC
))
913 DhcpOptionsProcess((u8
*)&bp
->bp_vend
[4], bp
);
914 /* Store net params from reply */
915 BootpCopyNetParams(bp
);
917 printf("DHCP client bound to address %pI4\n",
919 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP
,
927 /* DHCP client bound to address */
930 puts("DHCP: INVALID STATE\n");
936 void DhcpRequest(void)
940 #endif /* CONFIG_CMD_DHCP */