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 #ifdef CONFIG_BOOTP_RANDOM_DELAY
24 #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
27 * The timeout for the initial BOOTP/DHCP request used to be described by a
28 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
31 * Now that the timeout periods are variable (exponential backoff and retry)
32 * we convert the timeout count to the absolute time it would have take to
33 * execute that many retries, and keep sending retry packets until that time
36 #ifndef CONFIG_NET_RETRY_COUNT
37 # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
39 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
41 #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
43 #define PORT_BOOTPS 67 /* BOOTP server UDP port */
44 #define PORT_BOOTPC 68 /* BOOTP client UDP port */
46 #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
47 #define CONFIG_DHCP_MIN_EXT_LEN 64
50 #ifndef CONFIG_BOOTP_ID_CACHE_SIZE
51 #define CONFIG_BOOTP_ID_CACHE_SIZE 4
54 ulong bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
55 unsigned int bootp_num_ids;
60 #if defined(CONFIG_CMD_DHCP)
61 static dhcp_state_t dhcp_state = INIT;
62 static unsigned long dhcp_leasetime;
63 static struct in_addr dhcp_server_ip;
64 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
65 unsigned src, unsigned len);
69 static char *dhcpmsg2str(int type)
72 case 1: return "DHCPDISCOVER"; break;
73 case 2: return "DHCPOFFER"; break;
74 case 3: return "DHCPREQUEST"; break;
75 case 4: return "DHCPDECLINE"; break;
76 case 5: return "DHCPACK"; break;
77 case 6: return "DHCPNACK"; break;
78 case 7: return "DHCPRELEASE"; break;
79 default: return "UNKNOWN/INVALID MSG TYPE"; break;
85 static void bootp_add_id(ulong id)
87 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
88 size_t size = sizeof(bootp_ids) - sizeof(id);
90 memmove(bootp_ids, &bootp_ids[1], size);
91 bootp_ids[bootp_num_ids - 1] = id;
93 bootp_ids[bootp_num_ids] = id;
98 static bool bootp_match_id(ulong id)
102 for (i = 0; i < bootp_num_ids; i++)
103 if (bootp_ids[i] == id)
109 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
111 struct Bootp_t *bp = (struct Bootp_t *) pkt;
114 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
116 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
118 else if (bp->bp_op != OP_BOOTREQUEST &&
119 bp->bp_op != OP_BOOTREPLY &&
120 bp->bp_op != DHCP_OFFER &&
121 bp->bp_op != DHCP_ACK &&
122 bp->bp_op != DHCP_NAK)
124 else if (bp->bp_htype != HWT_ETHER)
126 else if (bp->bp_hlen != HWL_ETHER)
128 else if (!bootp_match_id(NetReadLong((ulong *)&bp->bp_id)))
131 debug("Filtering pkt = %d\n", retval);
137 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
139 static void BootpCopyNetParams(struct Bootp_t *bp)
141 #if !defined(CONFIG_BOOTP_SERVERIP)
142 struct in_addr tmp_ip;
144 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
145 if (tmp_ip.s_addr != 0)
146 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
147 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
148 if (strlen(bp->bp_file) > 0)
149 copy_filename(net_boot_file_name, bp->bp_file,
150 sizeof(net_boot_file_name));
152 debug("net_boot_file_name: %s\n", net_boot_file_name);
154 /* Propagate to environment:
155 * don't delete exising entry when BOOTP / DHCP reply does
156 * not contain a new value
158 if (*net_boot_file_name)
159 setenv("bootfile", net_boot_file_name);
161 net_copy_ip(&net_ip, &bp->bp_yiaddr);
164 static int truncate_sz(const char *name, int maxlen, int curlen)
166 if (curlen >= maxlen) {
167 printf("*** WARNING: %s is too long (%d - max: %d)"
168 " - truncated\n", name, curlen, maxlen);
174 #if !defined(CONFIG_CMD_DHCP)
176 static void BootpVendorFieldProcess(u8 *ext)
178 int size = *(ext + 1);
180 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
183 net_boot_file_expected_size_in_blocks = 0;
186 /* Fixed length fields */
187 case 1: /* Subnet mask */
188 if (net_netmask.s_addr == 0)
189 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
191 case 2: /* Time offset - Not yet supported */
193 /* Variable length fields */
194 case 3: /* Gateways list */
195 if (net_gateway.s_addr == 0)
196 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
198 case 4: /* Time server - Not yet supported */
200 case 5: /* IEN-116 name server - Not yet supported */
203 if (net_dns_server.s_addr == 0)
204 net_copy_ip(&net_dns_server,
205 (struct in_addr *)(ext + 2));
206 #if defined(CONFIG_BOOTP_DNS2)
207 if ((net_dns_server2.s_addr == 0) && (size > 4))
208 net_copy_ip(&net_dns_server2,
209 (struct in_addr *)(ext + 2 + 4));
212 case 7: /* Log server - Not yet supported */
214 case 8: /* Cookie/Quote server - Not yet supported */
216 case 9: /* LPR server - Not yet supported */
218 case 10: /* Impress server - Not yet supported */
220 case 11: /* RPL server - Not yet supported */
222 case 12: /* Host name */
223 if (NetOurHostName[0] == 0) {
224 size = truncate_sz("Host Name",
225 sizeof(NetOurHostName), size);
226 memcpy(&NetOurHostName, ext + 2, size);
227 NetOurHostName[size] = 0;
230 case 13: /* Boot file size */
232 net_boot_file_expected_size_in_blocks =
233 ntohs(*(ushort *)(ext + 2));
235 net_boot_file_expected_size_in_blocks =
236 ntohl(*(ulong *)(ext + 2));
238 case 14: /* Merit dump file - Not yet supported */
240 case 15: /* Domain name - Not yet supported */
242 case 16: /* Swap server - Not yet supported */
244 case 17: /* Root path */
245 if (NetOurRootPath[0] == 0) {
246 size = truncate_sz("Root Path",
247 sizeof(NetOurRootPath), size);
248 memcpy(&NetOurRootPath, ext + 2, size);
249 NetOurRootPath[size] = 0;
252 case 18: /* Extension path - Not yet supported */
254 * This can be used to send the information of the
255 * vendor area in another file that the client can
259 /* IP host layer fields */
260 case 40: /* NIS Domain name */
261 if (NetOurNISDomain[0] == 0) {
262 size = truncate_sz("NIS Domain Name",
263 sizeof(NetOurNISDomain), size);
264 memcpy(&NetOurNISDomain, ext + 2, size);
265 NetOurNISDomain[size] = 0;
268 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
269 case 42: /* NTP server IP */
270 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
273 /* Application layer fields */
274 case 43: /* Vendor specific info - Not yet supported */
276 * Binary information to exchange specific
277 * product information.
280 /* Reserved (custom) fields (128..254) */
284 static void BootpVendorProcess(u8 *ext, int size)
286 u8 *end = ext + size;
288 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
290 while ((ext < end) && (*ext != 0xff)) {
298 BootpVendorFieldProcess(opt);
302 debug("[BOOTP] Received fields:\n");
303 if (net_netmask.s_addr)
304 debug("net_netmask : %pI4\n", &net_netmask);
306 if (net_gateway.s_addr)
307 debug("net_gateway : %pI4", &net_gateway);
309 if (net_boot_file_expected_size_in_blocks)
310 debug("net_boot_file_expected_size_in_blocks : %d\n",
311 net_boot_file_expected_size_in_blocks);
313 if (NetOurHostName[0])
314 debug("NetOurHostName : %s\n", NetOurHostName);
316 if (NetOurRootPath[0])
317 debug("NetOurRootPath : %s\n", NetOurRootPath);
319 if (NetOurNISDomain[0])
320 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
322 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
324 debug("net_ntp_server : %pI4\n", &net_ntp_server);
329 * Handle a BOOTP received packet.
331 static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
332 unsigned src, unsigned len)
336 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
337 src, dest, len, sizeof(struct Bootp_t));
339 bp = (struct Bootp_t *)pkt;
341 /* Filter out pkts we don't want */
342 if (BootpCheckPkt(pkt, dest, src, len))
346 * Got a good BOOTP reply. Copy the data into our variables.
348 #ifdef CONFIG_STATUS_LED
349 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
352 BootpCopyNetParams(bp); /* Store net parameters from reply */
354 /* Retrieve extended information (we must parse the vendor area) */
355 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
356 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
358 NetSetTimeout(0, (thand_f *)0);
359 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
361 debug("Got good BOOTP\n");
368 * Timeout on BOOTP/DHCP request.
373 ulong time_taken = get_timer(bootp_start);
375 if (time_taken >= TIMEOUT_MS) {
376 #ifdef CONFIG_BOOTP_MAY_FAIL
377 puts("\nRetry time exceeded\n");
378 net_set_state(NETLOOP_FAIL);
380 puts("\nRetry time exceeded; starting again\n");
385 if (bootp_timeout > 2000)
386 bootp_timeout = 2000;
387 NetSetTimeout(bootp_timeout, BootpTimeout);
392 #define put_vci(e, str) \
394 size_t vci_strlen = strlen(str); \
395 *e++ = 60; /* Vendor Class Identifier */ \
397 memcpy(e, str, vci_strlen); \
402 * Initialize BOOTP extension fields in the request.
404 #if defined(CONFIG_CMD_DHCP)
405 static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
406 struct in_addr requested_ip)
410 #if defined(CONFIG_BOOTP_PXE)
415 #if defined(CONFIG_BOOTP_VENDOREX)
418 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
422 *e++ = 99; /* RFC1048 Magic Cookie */
427 *e++ = 53; /* DHCP Message Type */
431 *e++ = 57; /* Maximum DHCP Message Size */
433 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
434 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
436 if (server_ip.s_addr) {
437 int tmp = ntohl(server_ip.s_addr);
439 *e++ = 54; /* ServerID */
447 if (requested_ip.s_addr) {
448 int tmp = ntohl(requested_ip.s_addr);
450 *e++ = 50; /* Requested IP */
457 #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
458 hostname = getenv("hostname");
460 int hostnamelen = strlen(hostname);
462 *e++ = 12; /* Hostname */
464 memcpy(e, hostname, hostnamelen);
469 #if defined(CONFIG_BOOTP_PXE)
470 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
471 *e++ = 93; /* Client System Architecture */
473 *e++ = (clientarch >> 8) & 0xff;
474 *e++ = clientarch & 0xff;
476 *e++ = 94; /* Client Network Interface Identifier */
478 *e++ = 1; /* type field for UNDI */
479 *e++ = 0; /* major revision */
480 *e++ = 0; /* minor revision */
482 uuid = getenv("pxeuuid");
485 if (uuid_str_valid(uuid)) {
486 *e++ = 97; /* Client Machine Identifier */
488 *e++ = 0; /* type 0 - UUID */
490 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
493 printf("Invalid pxeuuid: %s\n", uuid);
498 #ifdef CONFIG_BOOTP_VCI_STRING
499 put_vci(e, CONFIG_BOOTP_VCI_STRING);
502 #if defined(CONFIG_BOOTP_VENDOREX)
503 x = dhcp_vendorex_prep(e);
508 *e++ = 55; /* Parameter Request List */
509 cnt = e++; /* Pointer to count of requested items */
511 #if defined(CONFIG_BOOTP_SUBNETMASK)
512 *e++ = 1; /* Subnet Mask */
515 #if defined(CONFIG_BOOTP_TIMEOFFSET)
519 #if defined(CONFIG_BOOTP_GATEWAY)
520 *e++ = 3; /* Router Option */
523 #if defined(CONFIG_BOOTP_DNS)
524 *e++ = 6; /* DNS Server(s) */
527 #if defined(CONFIG_BOOTP_HOSTNAME)
528 *e++ = 12; /* Hostname */
531 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
532 *e++ = 13; /* Boot File Size */
535 #if defined(CONFIG_BOOTP_BOOTPATH)
536 *e++ = 17; /* Boot path */
539 #if defined(CONFIG_BOOTP_NISDOMAIN)
540 *e++ = 40; /* NIS Domain name request */
543 #if defined(CONFIG_BOOTP_NTPSERVER)
547 /* no options, so back up to avoid sending an empty request list */
551 *e++ = 255; /* End of the list */
553 /* Pad to minimal length */
554 #ifdef CONFIG_DHCP_MIN_EXT_LEN
555 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
564 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
566 static int bootp_extended(u8 *e)
570 *e++ = 99; /* RFC1048 Magic Cookie */
575 #if defined(CONFIG_CMD_DHCP)
576 *e++ = 53; /* DHCP Message Type */
578 *e++ = DHCP_DISCOVER;
580 *e++ = 57; /* Maximum DHCP Message Size */
582 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
583 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
586 #if defined(CONFIG_BOOTP_VCI_STRING) || \
587 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
588 #ifdef CONFIG_SPL_BUILD
589 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
591 put_vci(e, CONFIG_BOOTP_VCI_STRING);
595 #if defined(CONFIG_BOOTP_SUBNETMASK)
596 *e++ = 1; /* Subnet mask request */
601 #if defined(CONFIG_BOOTP_GATEWAY)
602 *e++ = 3; /* Default gateway request */
607 #if defined(CONFIG_BOOTP_DNS)
608 *e++ = 6; /* Domain Name Server */
613 #if defined(CONFIG_BOOTP_HOSTNAME)
614 *e++ = 12; /* Host name request */
619 #if defined(CONFIG_BOOTP_BOOTFILESIZE)
620 *e++ = 13; /* Boot file size */
625 #if defined(CONFIG_BOOTP_BOOTPATH)
626 *e++ = 17; /* Boot path */
631 #if defined(CONFIG_BOOTP_NISDOMAIN)
632 *e++ = 40; /* NIS Domain name request */
636 #if defined(CONFIG_BOOTP_NTPSERVER)
642 *e++ = 255; /* End of the list */
648 void BootpReset(void)
652 bootp_start = get_timer(0);
661 int extlen, pktlen, iplen;
663 #ifdef CONFIG_BOOTP_RANDOM_DELAY
667 struct in_addr zero_ip;
668 struct in_addr bcast_ip;
670 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
671 #if defined(CONFIG_CMD_DHCP)
675 #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
679 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
680 rand_ms = rand() >> (22 - BootpTry);
681 else /* After 3rd BOOTP request max 8192 * 1ms */
682 rand_ms = rand() >> 19;
684 printf("Random delay: %ld ms...\n", rand_ms);
687 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
689 printf("BOOTP broadcast %d\n", ++BootpTry);
691 memset((void *)pkt, 0, PKTSIZE);
693 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
697 * Next line results in incorrect packet size being transmitted,
698 * resulting in errors in some DHCP servers, reporting missing bytes.
699 * Size must be set in packet header after extension length has been
701 * C. Hallinan, DS4.COM, Inc.
703 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
704 sizeof (struct Bootp_t)); */
705 iphdr = pkt; /* We need this later for net_set_udp_header() */
706 pkt += IP_UDP_HDR_SIZE;
708 bp = (struct Bootp_t *)pkt;
709 bp->bp_op = OP_BOOTREQUEST;
710 bp->bp_htype = HWT_ETHER;
711 bp->bp_hlen = HWL_ETHER;
713 bp->bp_secs = htons(get_timer(0) / 1000);
715 net_write_ip(&bp->bp_ciaddr, zero_ip);
716 net_write_ip(&bp->bp_yiaddr, zero_ip);
717 net_write_ip(&bp->bp_siaddr, zero_ip);
718 net_write_ip(&bp->bp_giaddr, zero_ip);
719 memcpy(bp->bp_chaddr, NetOurEther, 6);
720 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
722 /* Request additional information from the BOOTP/DHCP server */
723 #if defined(CONFIG_CMD_DHCP)
724 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
727 extlen = bootp_extended((u8 *)bp->bp_vend);
731 * Bootp ID is the lower 4 bytes of our ethernet address
732 * plus the current time in ms.
734 BootpID = ((ulong)NetOurEther[2] << 24)
735 | ((ulong)NetOurEther[3] << 16)
736 | ((ulong)NetOurEther[4] << 8)
737 | (ulong)NetOurEther[5];
738 BootpID += get_timer(0);
739 BootpID = htonl(BootpID);
740 bootp_add_id(BootpID);
741 NetCopyLong(&bp->bp_id, &BootpID);
744 * Calculate proper packet lengths taking into account the
745 * variable size of the options field
747 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
748 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
749 bcast_ip.s_addr = 0xFFFFFFFFL;
750 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
751 NetSetTimeout(bootp_timeout, BootpTimeout);
753 #if defined(CONFIG_CMD_DHCP)
754 dhcp_state = SELECTING;
755 net_set_udp_handler(dhcp_handler);
757 net_set_udp_handler(bootp_handler);
759 NetSendPacket(NetTxPacket, pktlen);
762 #if defined(CONFIG_CMD_DHCP)
763 static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
765 uchar *end = popt + BOOTP_HDR_SIZE;
767 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
771 while (popt < end && *popt != 0xff) {
775 net_copy_ip(&net_netmask, (popt + 2));
777 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
778 case 2: /* Time offset */
779 to_ptr = &NetTimeOffset;
780 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
781 NetTimeOffset = ntohl(NetTimeOffset);
785 net_copy_ip(&net_gateway, (popt + 2));
788 net_copy_ip(&net_dns_server, (popt + 2));
789 #if defined(CONFIG_BOOTP_DNS2)
791 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
795 size = truncate_sz("Host Name",
796 sizeof(NetOurHostName), oplen);
797 memcpy(&NetOurHostName, popt + 2, size);
798 NetOurHostName[size] = 0;
800 case 15: /* Ignore Domain Name Option */
803 size = truncate_sz("Root Path",
804 sizeof(NetOurRootPath), oplen);
805 memcpy(&NetOurRootPath, popt + 2, size);
806 NetOurRootPath[size] = 0;
808 case 28: /* Ignore Broadcast Address Option */
810 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
811 case 42: /* NTP server IP */
812 net_copy_ip(&net_ntp_server, (popt + 2));
816 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
818 case 53: /* Ignore Message Type Option */
821 net_copy_ip(&dhcp_server_ip, (popt + 2));
823 case 58: /* Ignore Renewal Time Option */
825 case 59: /* Ignore Rebinding Time Option */
827 case 66: /* Ignore TFTP server name */
829 case 67: /* vendor opt bootfile */
831 * I can't use dhcp_vendorex_proc here because I need
832 * to write into the bootp packet - even then I had to
833 * pass the bootp packet pointer into here as the
836 size = truncate_sz("Opt Boot File",
839 if (bp->bp_file[0] == '\0' && size > 0) {
841 * only use vendor boot file if we didn't
842 * receive a boot file in the main non-vendor
843 * part of the packet - god only knows why
844 * some vendors chose not to use this perfectly
845 * good spot to store the boot file (join on
846 * Tru64 Unix) it seems mind bogglingly crazy
849 printf("*** WARNING: using vendor "
850 "optional boot file\n");
851 memcpy(bp->bp_file, popt + 2, size);
852 bp->bp_file[size] = '\0';
856 #if defined(CONFIG_BOOTP_VENDOREX)
857 if (dhcp_vendorex_proc(popt))
860 printf("*** Unhandled DHCP Option in OFFER/ACK:"
864 popt += oplen + 2; /* Process next option */
868 static int DhcpMessageType(unsigned char *popt)
870 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
874 while (*popt != 0xff) {
875 if (*popt == 53) /* DHCP Message Type */
877 popt += *(popt + 1) + 2; /* Scan through all options */
882 static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
886 int pktlen, iplen, extlen;
888 struct in_addr offered_ip;
889 struct in_addr zero_ip;
890 struct in_addr bcast_ip;
892 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
894 memset((void *)pkt, 0, PKTSIZE);
896 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
899 iphdr = pkt; /* We'll need this later to set proper pkt size */
900 pkt += IP_UDP_HDR_SIZE;
902 bp = (struct Bootp_t *)pkt;
903 bp->bp_op = OP_BOOTREQUEST;
904 bp->bp_htype = HWT_ETHER;
905 bp->bp_hlen = HWL_ETHER;
907 bp->bp_secs = htons(get_timer(0) / 1000);
908 /* Do not set the client IP, your IP, or server IP yet, since it
909 * hasn't been ACK'ed by the server yet */
912 * RFC3046 requires Relay Agents to discard packets with
913 * nonzero and offered giaddr
916 net_write_ip(&bp->bp_giaddr, zero_ip);
918 memcpy(bp->bp_chaddr, NetOurEther, 6);
921 * ID is the id of the OFFER packet
924 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
927 * Copy options from OFFER packet if present
930 /* Copy offered IP into the parameters request list */
931 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
932 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
933 dhcp_server_ip, offered_ip);
935 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
936 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
937 bcast_ip.s_addr = 0xFFFFFFFFL;
938 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
940 #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
941 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
942 #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
943 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
944 NetSendPacket(NetTxPacket, pktlen);
948 * Handle DHCP received packets.
950 static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
951 unsigned src, unsigned len)
953 struct Bootp_t *bp = (struct Bootp_t *)pkt;
955 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
956 src, dest, len, dhcp_state);
958 /* Filter out pkts we don't want */
959 if (BootpCheckPkt(pkt, dest, src, len))
962 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
963 " %d\n", src, dest, len, dhcp_state);
965 switch (dhcp_state) {
968 * Wait an appropriate time for any potential DHCPOFFER packets
969 * to arrive. Then select one, and generate DHCPREQUEST
970 * response. If filename is in format we recognize, assume it
971 * is a valid OFFER from a server we want.
973 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
974 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
975 if (strncmp(bp->bp_file,
976 CONFIG_SYS_BOOTFILE_PREFIX,
977 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
978 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
980 debug("TRANSITIONING TO REQUESTING STATE\n");
981 dhcp_state = REQUESTING;
983 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
984 htonl(BOOTP_VENDOR_MAGIC))
985 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
987 NetSetTimeout(5000, BootpTimeout);
988 DhcpSendRequestPkt(bp);
989 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
991 #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
996 debug("DHCP State: REQUESTING\n");
998 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
999 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
1000 htonl(BOOTP_VENDOR_MAGIC))
1001 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
1002 /* Store net params from reply */
1003 BootpCopyNetParams(bp);
1005 printf("DHCP client bound to address %pI4 (%lu ms)\n",
1006 &net_ip, get_timer(bootp_start));
1007 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
1015 /* DHCP client bound to address */
1018 puts("DHCP: INVALID STATE\n");
1024 void DhcpRequest(void)
1028 #endif /* CONFIG_CMD_DHCP */