blob: 32852500d97e9f522a33e416a5851bb450d2081b [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
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
wdenk232c1502004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
12#include <command.h>
13#include <net.h>
14#include "bootp.h"
Joe Hershbergereafc8db2012-05-23 07:57:58 +000015#include "net_rand.h"
wdenk3861aa52002-09-27 23:19:37 +000016#include "tftp.h"
wdenk232c1502004-03-12 00:14:09 +000017#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000018#ifdef CONFIG_STATUS_LED
19#include <status_led.h>
20#endif
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +000021#include <linux/compiler.h>
wdenk3861aa52002-09-27 23:19:37 +000022
Joe Hershberger3090b7e32012-05-15 08:59:06 +000023#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000024
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020025#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000026#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000027# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000028#else
wdenk232c1502004-03-12 00:14:09 +000029# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000030#endif
31
Joe Hershberger3090b7e32012-05-15 08:59:06 +000032#define PORT_BOOTPS 67 /* BOOTP server UDP port */
33#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000034
Joe Hershberger3090b7e32012-05-15 08:59:06 +000035#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000036#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000037#endif
38
39ulong BootpID;
40int BootpTry;
wdenk3861aa52002-09-27 23:19:37 +000041
Jon Loeliger643d1ab2007-07-09 17:45:14 -050042#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000043dhcp_state_t dhcp_state = INIT;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000044unsigned long dhcp_leasetime;
45IPaddr_t NetDHCPServerIP;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000046static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
47 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000048
49/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000050#if 0
51static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000052{
53 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000054 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;
wdenk3861aa52002-09-27 23:19:37 +000061 default: return "UNKNOWN/INVALID MSG TYPE"; break;
62 }
63}
wdenk3e386912003-04-05 00:53:31 +000064#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050065#endif
wdenk3861aa52002-09-27 23:19:37 +000066
67static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
68{
Joe Hershberger3090b7e32012-05-15 08:59:06 +000069 struct Bootp_t *bp = (struct Bootp_t *) pkt;
wdenk3861aa52002-09-27 23:19:37 +000070 int retval = 0;
71
72 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
73 retval = -1;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000074 else if (len < sizeof(struct Bootp_t) - OPT_SIZE)
wdenk3861aa52002-09-27 23:19:37 +000075 retval = -2;
76 else if (bp->bp_op != OP_BOOTREQUEST &&
Joe Hershberger3090b7e32012-05-15 08:59:06 +000077 bp->bp_op != OP_BOOTREPLY &&
78 bp->bp_op != DHCP_OFFER &&
79 bp->bp_op != DHCP_ACK &&
80 bp->bp_op != DHCP_NAK)
wdenk3861aa52002-09-27 23:19:37 +000081 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +000082 else if (bp->bp_htype != HWT_ETHER)
83 retval = -4;
84 else if (bp->bp_hlen != HWL_ETHER)
85 retval = -5;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000086 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
wdenk3861aa52002-09-27 23:19:37 +000087 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +000088
Robin Getz0ebf04c2009-07-23 03:01:03 -040089 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000090
91 return retval;
92}
93
94/*
95 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
96 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +000097static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +000098{
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +000099 __maybe_unused IPaddr_t tmp_ip;
wdenk3d3befa2004-03-14 15:06:13 +0000100
wdenk3861aa52002-09-27 23:19:37 +0000101 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400102#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000103 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
104 if (tmp_ip != 0)
105 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershbergercb487f52012-05-23 07:58:06 +0000106 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400107#endif
wdenk3d3befa2004-03-14 15:06:13 +0000108 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000109 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000110
Robin Getz0ebf04c2009-07-23 03:01:03 -0400111 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000112
113 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000114 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000115 * not contain a new value
116 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000117 if (*BootFile)
118 setenv("bootfile", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000119}
120
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000121static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000122{
123 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000124 printf("*** WARNING: %s is too long (%d - max: %d)"
125 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000126 curlen = maxlen - 1;
127 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000128 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000129}
130
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500131#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000132
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000133static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000134{
wdenk232c1502004-03-12 00:14:09 +0000135 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000136
Robin Getz0ebf04c2009-07-23 03:01:03 -0400137 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000138 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000139
wdenk232c1502004-03-12 00:14:09 +0000140 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000141
wdenk232c1502004-03-12 00:14:09 +0000142 switch (*ext) {
143 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000144 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000145 if (NetOurSubnetMask == 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000146 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000147 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000148 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000149 break;
wdenk232c1502004-03-12 00:14:09 +0000150 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000151 case 3: /* Gateways list */
152 if (NetOurGatewayIP == 0)
153 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000154 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000155 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000156 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000157 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000158 break;
159 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000160 if (NetOurDNSIP == 0)
161 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500162#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000163 if ((NetOurDNS2IP == 0) && (size > 4))
164 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000165#endif
wdenk3861aa52002-09-27 23:19:37 +0000166 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000167 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000168 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000169 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000170 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000171 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000172 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000173 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000174 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000175 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000176 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000177 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000178 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000179 size = truncate_sz("Host Name",
180 sizeof(NetOurHostName), size);
181 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000182 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000183 }
184 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000185 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000186 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000187 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000188 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000189 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000190 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000191 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000192 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000193 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000194 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000195 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000196 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000197 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000198 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000199 size = truncate_sz("Root Path",
200 sizeof(NetOurRootPath), size);
201 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000202 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000203 }
204 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000205 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000206 /*
wdenk8bde7f72003-06-27 21:31:46 +0000207 * This can be used to send the information of the
208 * vendor area in another file that the client can
209 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000210 */
211 break;
wdenk232c1502004-03-12 00:14:09 +0000212 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000213 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000214 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000215 size = truncate_sz("NIS Domain Name",
216 sizeof(NetOurNISDomain), size);
217 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000218 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000219 }
220 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000221#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
wdenk232c1502004-03-12 00:14:09 +0000226 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000227 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000228 /*
wdenk8bde7f72003-06-27 21:31:46 +0000229 * Binary information to exchange specific
230 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000231 */
232 break;
wdenk232c1502004-03-12 00:14:09 +0000233 /* Reserved (custom) fields (128..254) */
234 }
wdenk3861aa52002-09-27 23:19:37 +0000235}
236
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000237static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000238{
wdenk232c1502004-03-12 00:14:09 +0000239 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000240
Robin Getz0ebf04c2009-07-23 03:01:03 -0400241 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000242
wdenk232c1502004-03-12 00:14:09 +0000243 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)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000251 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000252 }
wdenk3861aa52002-09-27 23:19:37 +0000253 }
wdenk3861aa52002-09-27 23:19:37 +0000254
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000255 debug("[BOOTP] Received fields:\n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500256 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400257 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000258
Mike Frysingerb6446b62009-02-17 00:00:53 -0500259 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400260 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000261
Robin Getz0ebf04c2009-07-23 03:01:03 -0400262 if (NetBootFileSize)
263 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000264
Robin Getz0ebf04c2009-07-23 03:01:03 -0400265 if (NetOurHostName[0])
266 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000267
Robin Getz0ebf04c2009-07-23 03:01:03 -0400268 if (NetOurRootPath[0])
269 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000270
Robin Getz0ebf04c2009-07-23 03:01:03 -0400271 if (NetOurNISDomain[0])
272 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000273
Robin Getz0ebf04c2009-07-23 03:01:03 -0400274 if (NetBootFileSize)
275 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000276
277#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
278 if (NetNtpServerIP)
279 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
280#endif
wdenk3861aa52002-09-27 23:19:37 +0000281}
Simon Glass09349862011-06-13 16:13:12 -0700282
wdenk3861aa52002-09-27 23:19:37 +0000283/*
284 * Handle a BOOTP received packet.
285 */
286static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000287BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
288 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000289{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000290 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000291
Robin Getz0ebf04c2009-07-23 03:01:03 -0400292 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000293 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000294
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000295 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000296
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000297 /* Filter out pkts we don't want */
298 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000299 return;
300
301 /*
wdenk232c1502004-03-12 00:14:09 +0000302 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000303 */
304#ifdef CONFIG_STATUS_LED
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000305 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000306#endif
307
308 BootpCopyNetParams(bp); /* Store net parameters from reply */
309
310 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000311 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200312 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000313
314 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000315 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000316
Robin Getz0ebf04c2009-07-23 03:01:03 -0400317 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000318
Simon Glasse4a3d572011-10-27 06:24:32 +0000319 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000320}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500321#endif
wdenk3861aa52002-09-27 23:19:37 +0000322
323/*
324 * Timeout on BOOTP/DHCP request.
325 */
326static void
327BootpTimeout(void)
328{
329 if (BootpTry >= TIMEOUT_COUNT) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000330 puts("\nRetry count exceeded; starting again\n");
331 NetStartAgain();
wdenk3861aa52002-09-27 23:19:37 +0000332 } else {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000333 NetSetTimeout(TIMEOUT, BootpTimeout);
334 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000335 }
336}
337
338/*
339 * Initialize BOOTP extension fields in the request.
340 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500341#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000342static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
343 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000344{
wdenk232c1502004-03-12 00:14:09 +0000345 u8 *start = e;
346 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000347#if defined(CONFIG_BOOTP_PXE)
348 char *uuid;
349 size_t vci_strlen;
350 u16 clientarch;
351#endif
wdenk232c1502004-03-12 00:14:09 +0000352
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500353#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000354 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000355#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500356#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200357 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000358#endif
wdenk3861aa52002-09-27 23:19:37 +0000359
wdenk232c1502004-03-12 00:14:09 +0000360 *e++ = 99; /* RFC1048 Magic Cookie */
361 *e++ = 130;
362 *e++ = 83;
363 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000364
wdenk232c1502004-03-12 00:14:09 +0000365 *e++ = 53; /* DHCP Message Type */
366 *e++ = 1;
367 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000368
wdenk232c1502004-03-12 00:14:09 +0000369 *e++ = 57; /* Maximum DHCP Message Size */
370 *e++ = 2;
371 *e++ = (576 - 312 + OPT_SIZE) >> 8;
372 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000373
wdenk232c1502004-03-12 00:14:09 +0000374 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000375 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000376
wdenk232c1502004-03-12 00:14:09 +0000377 *e++ = 54; /* ServerID */
378 *e++ = 4;
379 *e++ = tmp >> 24;
380 *e++ = tmp >> 16;
381 *e++ = tmp >> 8;
382 *e++ = tmp & 0xff;
383 }
wdenk3861aa52002-09-27 23:19:37 +0000384
wdenk232c1502004-03-12 00:14:09 +0000385 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000386 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000387
wdenk232c1502004-03-12 00:14:09 +0000388 *e++ = 50; /* Requested IP */
389 *e++ = 4;
390 *e++ = tmp >> 24;
391 *e++ = tmp >> 16;
392 *e++ = tmp >> 8;
393 *e++ = tmp & 0xff;
394 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500395#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000396 hostname = getenv("hostname");
397 if (hostname) {
398 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000399
400 *e++ = 12; /* Hostname */
401 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000402 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000403 e += hostnamelen;
404 }
stroesefe389a82003-08-28 14:17:32 +0000405#endif
406
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000407#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
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500442#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000443 x = dhcp_vendorex_prep(e);
444 if (x)
wdenk232c1502004-03-12 00:14:09 +0000445 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000446#endif
447
wdenk232c1502004-03-12 00:14:09 +0000448 *e++ = 55; /* Parameter Request List */
449 cnt = e++; /* Pointer to count of requested items */
450 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500451#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000452 *e++ = 1; /* Subnet Mask */
453 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000454#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500455#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000456 *e++ = 2;
457 *cnt += 1;
458#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500459#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000460 *e++ = 3; /* Router Option */
461 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000462#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500463#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 6; /* DNS Server(s) */
465 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000466#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000468 *e++ = 12; /* Hostname */
469 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000470#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500471#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 13; /* Boot File Size */
473 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500475#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000476 *e++ = 17; /* Boot path */
477 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000478#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500479#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000480 *e++ = 40; /* NIS Domain name request */
481 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000482#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500483#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000484 *e++ = 42;
485 *cnt += 1;
486#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800487 /* no options, so back up to avoid sending an empty request list */
488 if (*cnt == 0)
489 e -= 2;
490
wdenk232c1502004-03-12 00:14:09 +0000491 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000492
wdenk232c1502004-03-12 00:14:09 +0000493 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000494#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800495 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000496 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000497#endif
498
wdenk232c1502004-03-12 00:14:09 +0000499 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000500}
501
Jon Loeliger610f2e92007-07-10 11:05:02 -0500502#else
wdenk3861aa52002-09-27 23:19:37 +0000503/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000504 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000505 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000506static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000507{
wdenk232c1502004-03-12 00:14:09 +0000508 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000509
wdenk232c1502004-03-12 00:14:09 +0000510 *e++ = 99; /* RFC1048 Magic Cookie */
511 *e++ = 130;
512 *e++ = 83;
513 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000514
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500515#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000516 *e++ = 53; /* DHCP Message Type */
517 *e++ = 1;
518 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000519
wdenk232c1502004-03-12 00:14:09 +0000520 *e++ = 57; /* Maximum DHCP Message Size */
521 *e++ = 2;
522 *e++ = (576 - 312 + OPT_SIZE) >> 16;
523 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500524#endif
wdenk3861aa52002-09-27 23:19:37 +0000525
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500526#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000527 *e++ = 1; /* Subnet mask request */
528 *e++ = 4;
529 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000530#endif
531
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500532#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000533 *e++ = 3; /* Default gateway request */
534 *e++ = 4;
535 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
537
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500538#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000539 *e++ = 6; /* Domain Name Server */
540 *e++ = 4;
541 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000542#endif
543
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500544#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000545 *e++ = 12; /* Host name request */
546 *e++ = 32;
547 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000548#endif
549
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500550#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000551 *e++ = 13; /* Boot file size */
552 *e++ = 2;
553 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000554#endif
555
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500556#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000557 *e++ = 17; /* Boot path */
558 *e++ = 32;
559 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000560#endif
561
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500562#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000563 *e++ = 40; /* NIS Domain name request */
564 *e++ = 32;
565 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000566#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000567#if defined(CONFIG_BOOTP_NTPSERVER)
568 *e++ = 42;
569 *e++ = 4;
570 e += 4;
571#endif
wdenk3861aa52002-09-27 23:19:37 +0000572
wdenk232c1502004-03-12 00:14:09 +0000573 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000574
wdenk232c1502004-03-12 00:14:09 +0000575 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000576}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500577#endif
wdenk3861aa52002-09-27 23:19:37 +0000578
579void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000580BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000581{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000582 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000583 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000584 int ext_len, pktlen, iplen;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000585#ifdef CONFIG_BOOTP_RANDOM_DELAY
586 ulong i, rand_ms;
587#endif
wdenk3861aa52002-09-27 23:19:37 +0000588
Simon Glass573f14f2011-12-10 11:08:06 +0000589 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500590#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000591 dhcp_state = INIT;
592#endif
593
594#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000595 if (BootpTry == 0)
596 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000597
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000598 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;
wdenk3861aa52002-09-27 23:19:37 +0000602
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000603 printf("Random delay: %ld ms...\n", rand_ms);
604 for (i = 0; i < rand_ms; i++)
wdenk3861aa52002-09-27 23:19:37 +0000605 udelay(1000); /*Wait 1ms*/
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000606
wdenk3861aa52002-09-27 23:19:37 +0000607#endif /* CONFIG_BOOTP_RANDOM_DELAY */
608
609 printf("BOOTP broadcast %d\n", ++BootpTry);
610 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000611 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000612
wdenka3d991b2004-04-15 21:48:45 +0000613 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000614
615 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000616 * 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.
wdenk3861aa52002-09-27 23:19:37 +0000620 * C. Hallinan, DS4.COM, Inc.
621 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000622 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
623 sizeof (struct Bootp_t)); */
wdenk3861aa52002-09-27 23:19:37 +0000624 iphdr = pkt; /* We need this later for NetSetIP() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000625 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000626
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000627 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000628 bp->bp_op = OP_BOOTREQUEST;
629 bp->bp_htype = HWT_ETHER;
630 bp->bp_hlen = HWL_ETHER;
631 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200632 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000633 NetWriteIP(&bp->bp_ciaddr, 0);
634 NetWriteIP(&bp->bp_yiaddr, 0);
635 NetWriteIP(&bp->bp_siaddr, 0);
636 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000637 memcpy(bp->bp_chaddr, NetOurEther, 6);
638 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000639
640 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500641#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200642 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000643#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200644 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500645#endif
wdenk3861aa52002-09-27 23:19:37 +0000646
647 /*
648 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200649 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000650 */
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);
wdenk232c1502004-03-12 00:14:09 +0000656 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000657 NetCopyLong(&bp->bp_id, &BootpID);
658
659 /*
660 * Calculate proper packet lengths taking into account the
661 * variable size of the options field
662 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000663 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
664 sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000665 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
666 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200667 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000668
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500669#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000670 dhcp_state = SELECTING;
671 NetSetHandler(DhcpHandler);
672#else
673 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500674#endif
wdenk3861aa52002-09-27 23:19:37 +0000675 NetSendPacket(NetTxPacket, pktlen);
676}
677
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500678#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000679static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000680{
wdenk3e386912003-04-05 00:53:31 +0000681 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000682 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200683#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
684 int *to_ptr;
685#endif
wdenk3861aa52002-09-27 23:19:37 +0000686
wdenk232c1502004-03-12 00:14:09 +0000687 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000688 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000689 switch (*popt) {
690 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000691 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000692 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500693#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000694 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200695 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000696 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
697 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000698 break;
699#endif
wdenk232c1502004-03-12 00:14:09 +0000700 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000701 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000702 break;
703 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000704 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500705#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000706 if (*(popt + 1) > 4)
707 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000708#endif
wdenk232c1502004-03-12 00:14:09 +0000709 break;
710 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000711 size = truncate_sz("Host Name",
712 sizeof(NetOurHostName), oplen);
713 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000714 NetOurHostName[size] = 0;
715 break;
716 case 15: /* Ignore Domain Name Option */
717 break;
718 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000719 size = truncate_sz("Root Path",
720 sizeof(NetOurRootPath), oplen);
721 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000722 NetOurRootPath[size] = 0;
723 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500724#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000725 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000726 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000727 break;
728#endif
wdenk232c1502004-03-12 00:14:09 +0000729 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000730 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000731 break;
732 case 53: /* Ignore Message Type Option */
733 break;
734 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000735 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000736 break;
737 case 58: /* Ignore Renewal Time Option */
738 break;
739 case 59: /* Ignore Rebinding Time Option */
740 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100741 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 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000750 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100751 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;
wdenk232c1502004-03-12 00:14:09 +0000769 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500770#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000771 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000772 break;
wdenk3861aa52002-09-27 23:19:37 +0000773#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000774 printf("*** Unhandled DHCP Option in OFFER/ACK:"
775 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000776 break;
wdenk3861aa52002-09-27 23:19:37 +0000777 }
778 popt += oplen + 2; /* Process next option */
779 }
780}
781
782static int DhcpMessageType(unsigned char *popt)
783{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000784 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000785 return -1;
786
787 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000788 while (*popt != 0xff) {
789 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000790 return *(popt + 2);
791 popt += *(popt + 1) + 2; /* Scan through all options */
792 }
793 return -1;
794}
795
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000796static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000797{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000798 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000799 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000800 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000801 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000802
Robin Getz0ebf04c2009-07-23 03:01:03 -0400803 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000804 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000805 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000806
wdenka3d991b2004-04-15 21:48:45 +0000807 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000808
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000809 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000810 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000811
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000812 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000813 bp->bp_op = OP_BOOTREQUEST;
814 bp->bp_htype = HWT_ETHER;
815 bp->bp_hlen = HWL_ETHER;
816 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200817 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000818 /* Do not set the client IP, your IP, or server IP yet, since it
819 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400820
Wolfgang Denkc6686702006-10-12 00:01:08 +0200821 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200822 * RFC3046 requires Relay Agents to discard packets with
823 * nonzero and offered giaddr
824 */
825 NetWriteIP(&bp->bp_giaddr, 0);
826
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000827 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000828
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 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400838
839 /* Copy offered IP into the parameters request list */
840 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000841 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
842 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000843
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000844 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
845 sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000846 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
847 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
848
Robin Getz0ebf04c2009-07-23 03:01:03 -0400849 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100850#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
851 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
852#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000853 NetSendPacket(NetTxPacket, pktlen);
854}
855
856/*
857 * Handle DHCP received packets.
858 */
859static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000860DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
861 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000862{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000863 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000864
Robin Getz0ebf04c2009-07-23 03:01:03 -0400865 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000866 src, dest, len, dhcp_state);
867
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000868 /* Filter out pkts we don't want */
869 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000870 return;
871
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000872 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
873 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000874
875 switch (dhcp_state) {
876 case SELECTING:
877 /*
878 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000879 * 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.
wdenk3861aa52002-09-27 23:19:37 +0000882 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400883 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200884#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000885 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200886 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000887 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200888#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000889
Robin Getz0ebf04c2009-07-23 03:01:03 -0400890 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000891 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000892
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000893 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
894 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100895 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000896
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200897 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000898 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200899#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000900 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200901#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000902
903 return;
904 break;
905 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400906 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000907
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000908 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
909 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
910 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100911 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000912 /* Store net params from reply */
913 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000914 dhcp_state = BOUND;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000915 printf("DHCP client bound to address %pI4\n",
916 &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000917 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000918 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000919
Simon Glasse4a3d572011-10-27 06:24:32 +0000920 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000921 return;
922 }
923 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200924 case BOUND:
925 /* DHCP client bound to address */
926 break;
wdenk3861aa52002-09-27 23:19:37 +0000927 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000928 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000929 break;
930 }
931
932}
933
934void DhcpRequest(void)
935{
936 BootpRequest();
937}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100938#endif /* CONFIG_CMD_DHCP */