blob: 73470f2d46fc66942b65e39a975be89e43329f4a [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
wdenk29e7f5a2004-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"
15#include "tftp.h"
wdenk29e7f5a2004-03-12 00:14:09 +000016#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000017#ifdef CONFIG_STATUS_LED
18#include <status_led.h>
19#endif
20
wdenk29e7f5a2004-03-12 00:14:09 +000021#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000022
Bartlomiej Sieka56668462008-10-01 15:26:28 +020023#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk29e7f5a2004-03-12 00:14:09 +000024#ifndef CONFIG_NET_RETRY_COUNT
wdenk3861aa52002-09-27 23:19:37 +000025# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
26#else
wdenk29e7f5a2004-03-12 00:14:09 +000027# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000028#endif
29
30#define PORT_BOOTPS 67 /* BOOTP server UDP port */
31#define PORT_BOOTPC 68 /* BOOTP client UDP port */
32
33#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk29e7f5a2004-03-12 00:14:09 +000034#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000035#endif
36
37ulong BootpID;
38int BootpTry;
39#ifdef CONFIG_BOOTP_RANDOM_DELAY
40ulong seed1, seed2;
41#endif
42
Jon Loeliger54f35c22007-07-09 17:45:14 -050043#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000044dhcp_state_t dhcp_state = INIT;
wdenk4e112c12003-06-03 23:54:09 +000045unsigned long dhcp_leasetime = 0;
stroese5fa6e902003-04-10 13:26:44 +000046IPaddr_t NetDHCPServerIP = 0;
Luca Ceresoli428ab362011-04-18 06:19:50 +000047static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
48 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000049
50/* For Debug */
wdenkb02744a2003-04-05 00:53:31 +000051#if 0
52static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000053{
54 switch (type) {
wdenk29e7f5a2004-03-12 00:14:09 +000055 case 1: return "DHCPDISCOVER"; break;
56 case 2: return "DHCPOFFER"; break;
57 case 3: return "DHCPREQUEST"; break;
58 case 4: return "DHCPDECLINE"; break;
59 case 5: return "DHCPACK"; break;
60 case 6: return "DHCPNACK"; break;
61 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000062 default: return "UNKNOWN/INVALID MSG TYPE"; break;
63 }
64}
wdenkb02744a2003-04-05 00:53:31 +000065#endif
wdenk3861aa52002-09-27 23:19:37 +000066
Jon Loeliger5336a762007-07-09 22:08:34 -050067#if defined(CONFIG_BOOTP_VENDOREX)
wdenk3861aa52002-09-27 23:19:37 +000068extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
69extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
70#endif
71
Jon Loeligera9807e52007-07-10 11:05:02 -050072#endif
wdenk3861aa52002-09-27 23:19:37 +000073
74static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
75{
76 Bootp_t *bp = (Bootp_t *) pkt;
77 int retval = 0;
78
79 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
80 retval = -1;
81 else if (len < sizeof (Bootp_t) - OPT_SIZE)
82 retval = -2;
83 else if (bp->bp_op != OP_BOOTREQUEST &&
84 bp->bp_op != OP_BOOTREPLY &&
85 bp->bp_op != DHCP_OFFER &&
86 bp->bp_op != DHCP_ACK &&
87 bp->bp_op != DHCP_NAK ) {
88 retval = -3;
89 }
90 else if (bp->bp_htype != HWT_ETHER)
91 retval = -4;
92 else if (bp->bp_hlen != HWL_ETHER)
93 retval = -5;
94 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
95 retval = -6;
96 }
97
Robin Getz9e0a4d62009-07-23 03:01:03 -040098 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000099
100 return retval;
101}
102
103/*
104 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
105 */
wdenkb02744a2003-04-05 00:53:31 +0000106static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000107{
wdenk4989f872004-03-14 15:06:13 +0000108 IPaddr_t tmp_ip;
109
wdenk3861aa52002-09-27 23:19:37 +0000110 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan22bcd6e2007-07-28 10:56:13 -0400111#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk4989f872004-03-14 15:06:13 +0000112 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
113 if (tmp_ip != 0)
114 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Mike Frysingerdc166032009-07-18 21:04:08 -0400115 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan22bcd6e2007-07-28 10:56:13 -0400116#endif
wdenk4989f872004-03-14 15:06:13 +0000117 if (strlen(bp->bp_file) > 0)
118 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000119
Robin Getz9e0a4d62009-07-23 03:01:03 -0400120 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000121
122 /* Propagate to environment:
wdenk57b2d802003-06-27 21:31:46 +0000123 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000124 * not contain a new value
125 */
126 if (*BootFile) {
127 setenv ("bootfile", BootFile);
128 }
129}
130
131static int truncate_sz (const char *name, int maxlen, int curlen)
132{
133 if (curlen >= maxlen) {
134 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
135 name, curlen, maxlen);
136 curlen = maxlen - 1;
137 }
138 return (curlen);
139}
140
Simon Glassab068eb2011-06-13 16:13:12 -0700141/*
142 * Check if autoload is enabled. If so, use either NFS or TFTP to download
143 * the boot file.
144 */
145static void auto_load(void)
146{
147 const char *s = getenv("autoload");
148
149 if (s != NULL) {
150 if (*s == 'n') {
151 /*
152 * Just use BOOTP to configure system;
153 * Do not use TFTP to load the bootfile.
154 */
155 NetState = NETLOOP_SUCCESS;
156 return;
157 }
158#if defined(CONFIG_CMD_NFS)
159 if (strcmp(s, "NFS") == 0) {
160 /*
161 * Use NFS to load the bootfile.
162 */
163 NfsStart();
164 return;
165 }
166#endif
Simon Glassab068eb2011-06-13 16:13:12 -0700167 }
Peter Korsgaard10b532c2011-09-18 21:54:46 +0000168 TftpStart();
Simon Glassab068eb2011-06-13 16:13:12 -0700169}
170
Jon Loeliger54f35c22007-07-09 17:45:14 -0500171#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000172
wdenk29e7f5a2004-03-12 00:14:09 +0000173static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000174{
wdenk29e7f5a2004-03-12 00:14:09 +0000175 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000176
Robin Getz9e0a4d62009-07-23 03:01:03 -0400177 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
wdenk29e7f5a2004-03-12 00:14:09 +0000178 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000179
wdenk29e7f5a2004-03-12 00:14:09 +0000180 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000181
wdenk29e7f5a2004-03-12 00:14:09 +0000182 switch (*ext) {
183 /* Fixed length fields */
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700184 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000185 if (NetOurSubnetMask == 0)
wdenk29e7f5a2004-03-12 00:14:09 +0000186 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000187 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700188 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000189 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000190 /* Variable length fields */
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700191 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000192 if (NetOurGatewayIP == 0) {
wdenk29e7f5a2004-03-12 00:14:09 +0000193 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000194 }
195 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700196 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000197 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700198 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000199 break;
200 case 6:
201 if (NetOurDNSIP == 0) {
wdenk29e7f5a2004-03-12 00:14:09 +0000202 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000203 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500204#if defined(CONFIG_BOOTP_DNS2)
stroesee0aadfb2003-08-28 14:17:32 +0000205 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk29e7f5a2004-03-12 00:14:09 +0000206 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000207 }
208#endif
wdenk3861aa52002-09-27 23:19:37 +0000209 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700210 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000211 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700212 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000213 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700214 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000215 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700216 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000217 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700218 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000219 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700220 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000221 if (NetOurHostName[0] == 0) {
wdenk29e7f5a2004-03-12 00:14:09 +0000222 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
223 memcpy (&NetOurHostName, ext + 2, size);
224 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000225 }
226 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700227 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000228 if (size == 2)
wdenk29e7f5a2004-03-12 00:14:09 +0000229 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000230 else if (size == 4)
wdenk29e7f5a2004-03-12 00:14:09 +0000231 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000232 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700233 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000234 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700235 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000236 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700237 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000238 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700239 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000240 if (NetOurRootPath[0] == 0) {
wdenk29e7f5a2004-03-12 00:14:09 +0000241 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
242 memcpy (&NetOurRootPath, ext + 2, size);
243 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000244 }
245 break;
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700246 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000247 /*
wdenk57b2d802003-06-27 21:31:46 +0000248 * This can be used to send the information of the
249 * vendor area in another file that the client can
250 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000251 */
252 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000253 /* IP host layer fields */
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700254 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000255 if (NetOurNISDomain[0] == 0) {
wdenk29e7f5a2004-03-12 00:14:09 +0000256 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
257 memcpy (&NetOurNISDomain, ext + 2, size);
258 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000259 }
260 break;
Luuk Paulussen6380e012011-05-16 18:29:19 +0000261#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
262 case 42: /* NTP server IP */
263 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
264 break;
265#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000266 /* Application layer fields */
Wolfgang Denk35f734f2008-04-13 09:59:26 -0700267 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000268 /*
wdenk57b2d802003-06-27 21:31:46 +0000269 * Binary information to exchange specific
270 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000271 */
272 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000273 /* Reserved (custom) fields (128..254) */
274 }
wdenk3861aa52002-09-27 23:19:37 +0000275}
276
wdenk29e7f5a2004-03-12 00:14:09 +0000277static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000278{
wdenk29e7f5a2004-03-12 00:14:09 +0000279 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000280
Robin Getz9e0a4d62009-07-23 03:01:03 -0400281 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000282
wdenk29e7f5a2004-03-12 00:14:09 +0000283 while ((ext < end) && (*ext != 0xff)) {
284 if (*ext == 0) {
285 ext++;
286 } else {
287 u8 *opt = ext;
288
289 ext += ext[1] + 2;
290 if (ext <= end)
291 BootpVendorFieldProcess (opt);
292 }
wdenk3861aa52002-09-27 23:19:37 +0000293 }
wdenk3861aa52002-09-27 23:19:37 +0000294
Robin Getz9e0a4d62009-07-23 03:01:03 -0400295 debug("[BOOTP] Received fields: \n");
Mike Frysingerd5695f42009-02-17 00:00:53 -0500296 if (NetOurSubnetMask)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400297 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000298
Mike Frysingerd5695f42009-02-17 00:00:53 -0500299 if (NetOurGatewayIP)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400300 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000301
Robin Getz9e0a4d62009-07-23 03:01:03 -0400302 if (NetBootFileSize)
303 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000304
Robin Getz9e0a4d62009-07-23 03:01:03 -0400305 if (NetOurHostName[0])
306 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000307
Robin Getz9e0a4d62009-07-23 03:01:03 -0400308 if (NetOurRootPath[0])
309 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000310
Robin Getz9e0a4d62009-07-23 03:01:03 -0400311 if (NetOurNISDomain[0])
312 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000313
Robin Getz9e0a4d62009-07-23 03:01:03 -0400314 if (NetBootFileSize)
315 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen6380e012011-05-16 18:29:19 +0000316
317#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
318 if (NetNtpServerIP)
319 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
320#endif
wdenk3861aa52002-09-27 23:19:37 +0000321}
Simon Glassab068eb2011-06-13 16:13:12 -0700322
wdenk3861aa52002-09-27 23:19:37 +0000323/*
324 * Handle a BOOTP received packet.
325 */
326static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000327BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
328 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000329{
330 Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000331
Robin Getz9e0a4d62009-07-23 03:01:03 -0400332 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000333 src, dest, len, sizeof (Bootp_t));
334
335 bp = (Bootp_t *)pkt;
336
wdenk29e7f5a2004-03-12 00:14:09 +0000337 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000338 return;
339
340 /*
wdenk29e7f5a2004-03-12 00:14:09 +0000341 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000342 */
343#ifdef CONFIG_STATUS_LED
344 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
345#endif
346
347 BootpCopyNetParams(bp); /* Store net parameters from reply */
348
349 /* Retrieve extended information (we must parse the vendor area) */
350 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200351 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000352
353 NetSetTimeout(0, (thand_f *)0);
354
Robin Getz9e0a4d62009-07-23 03:01:03 -0400355 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000356
Simon Glassab068eb2011-06-13 16:13:12 -0700357 auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000358}
Jon Loeligera9807e52007-07-10 11:05:02 -0500359#endif
wdenk3861aa52002-09-27 23:19:37 +0000360
361/*
362 * Timeout on BOOTP/DHCP request.
363 */
364static void
365BootpTimeout(void)
366{
367 if (BootpTry >= TIMEOUT_COUNT) {
368 puts ("\nRetry count exceeded; starting again\n");
369 NetStartAgain ();
370 } else {
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200371 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000372 BootpRequest ();
373 }
374}
375
376/*
377 * Initialize BOOTP extension fields in the request.
378 */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500379#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000380static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000381{
wdenk29e7f5a2004-03-12 00:14:09 +0000382 u8 *start = e;
383 u8 *cnt;
Jason Hobbsea3202812011-08-31 05:37:31 +0000384#if defined(CONFIG_BOOTP_PXE)
385 char *uuid;
386 size_t vci_strlen;
387 u16 clientarch;
388#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000389
Jon Loeliger5336a762007-07-09 22:08:34 -0500390#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000391 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000392#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500393#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200394 char *hostname;
stroesee0aadfb2003-08-28 14:17:32 +0000395#endif
wdenk3861aa52002-09-27 23:19:37 +0000396
wdenk29e7f5a2004-03-12 00:14:09 +0000397 *e++ = 99; /* RFC1048 Magic Cookie */
398 *e++ = 130;
399 *e++ = 83;
400 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000401
wdenk29e7f5a2004-03-12 00:14:09 +0000402 *e++ = 53; /* DHCP Message Type */
403 *e++ = 1;
404 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000405
wdenk29e7f5a2004-03-12 00:14:09 +0000406 *e++ = 57; /* Maximum DHCP Message Size */
407 *e++ = 2;
408 *e++ = (576 - 312 + OPT_SIZE) >> 8;
409 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000410
wdenk29e7f5a2004-03-12 00:14:09 +0000411 if (ServerID) {
412 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000413
wdenk29e7f5a2004-03-12 00:14:09 +0000414 *e++ = 54; /* ServerID */
415 *e++ = 4;
416 *e++ = tmp >> 24;
417 *e++ = tmp >> 16;
418 *e++ = tmp >> 8;
419 *e++ = tmp & 0xff;
420 }
wdenk3861aa52002-09-27 23:19:37 +0000421
wdenk29e7f5a2004-03-12 00:14:09 +0000422 if (RequestedIP) {
423 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000424
wdenk29e7f5a2004-03-12 00:14:09 +0000425 *e++ = 50; /* Requested IP */
426 *e++ = 4;
427 *e++ = tmp >> 24;
428 *e++ = tmp >> 16;
429 *e++ = tmp >> 8;
430 *e++ = tmp & 0xff;
431 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500432#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000433 if ((hostname = getenv ("hostname"))) {
434 int hostnamelen = strlen (hostname);
435
436 *e++ = 12; /* Hostname */
437 *e++ = hostnamelen;
438 memcpy (e, hostname, hostnamelen);
439 e += hostnamelen;
440 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000441#endif
442
443#if defined(CONFIG_BOOTP_PXE)
444 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
445 *e++ = 93; /* Client System Architecture */
446 *e++ = 2;
447 *e++ = (clientarch >> 8) & 0xff;
448 *e++ = clientarch & 0xff;
449
450 *e++ = 94; /* Client Network Interface Identifier */
451 *e++ = 3;
452 *e++ = 1; /* type field for UNDI */
453 *e++ = 0; /* major revision */
454 *e++ = 0; /* minor revision */
455
456 uuid = getenv("pxeuuid");
457
458 if (uuid) {
459 if (uuid_str_valid(uuid)) {
460 *e++ = 97; /* Client Machine Identifier */
461 *e++ = 17;
462 *e++ = 0; /* type 0 - UUID */
463
464 uuid_str_to_bin(uuid, e);
465 e += 16;
466 } else {
467 printf("Invalid pxeuuid: %s\n", uuid);
468 }
469 }
470
471 *e++ = 60; /* Vendor Class Identifier */
472 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
473 *e++ = vci_strlen;
474 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
475 e += vci_strlen;
stroesee0aadfb2003-08-28 14:17:32 +0000476#endif
477
Jon Loeliger5336a762007-07-09 22:08:34 -0500478#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000479 if ((x = dhcp_vendorex_prep (e)))
480 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000481#endif
482
wdenk29e7f5a2004-03-12 00:14:09 +0000483 *e++ = 55; /* Parameter Request List */
484 cnt = e++; /* Pointer to count of requested items */
485 *cnt = 0;
Jon Loeliger5336a762007-07-09 22:08:34 -0500486#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000487 *e++ = 1; /* Subnet Mask */
488 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000489#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500490#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000491 *e++ = 2;
492 *cnt += 1;
493#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500494#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000495 *e++ = 3; /* Router Option */
496 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000497#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500498#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000499 *e++ = 6; /* DNS Server(s) */
500 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000501#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500502#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000503 *e++ = 12; /* Hostname */
504 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000505#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500506#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000507 *e++ = 13; /* Boot File Size */
508 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000509#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500510#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000511 *e++ = 17; /* Boot path */
512 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000513#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500514#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000515 *e++ = 40; /* NIS Domain name request */
516 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000517#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500518#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000519 *e++ = 42;
520 *cnt += 1;
521#endif
Jason Liu1c7dca52010-11-14 12:23:09 +0800522 /* no options, so back up to avoid sending an empty request list */
523 if (*cnt == 0)
524 e -= 2;
525
wdenk29e7f5a2004-03-12 00:14:09 +0000526 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000527
wdenk29e7f5a2004-03-12 00:14:09 +0000528 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000529#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glassa0978eb2011-02-02 15:03:28 -0800530 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk29e7f5a2004-03-12 00:14:09 +0000531 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000532#endif
533
wdenk29e7f5a2004-03-12 00:14:09 +0000534 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000535}
536
Jon Loeligera9807e52007-07-10 11:05:02 -0500537#else
wdenk3861aa52002-09-27 23:19:37 +0000538/*
Jon Loeliger5336a762007-07-09 22:08:34 -0500539 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000540 */
wdenk29e7f5a2004-03-12 00:14:09 +0000541static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000542{
wdenk29e7f5a2004-03-12 00:14:09 +0000543 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000544
wdenk29e7f5a2004-03-12 00:14:09 +0000545 *e++ = 99; /* RFC1048 Magic Cookie */
546 *e++ = 130;
547 *e++ = 83;
548 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000549
Jon Loeliger54f35c22007-07-09 17:45:14 -0500550#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000551 *e++ = 53; /* DHCP Message Type */
552 *e++ = 1;
553 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000554
wdenk29e7f5a2004-03-12 00:14:09 +0000555 *e++ = 57; /* Maximum DHCP Message Size */
556 *e++ = 2;
557 *e++ = (576 - 312 + OPT_SIZE) >> 16;
558 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeligera9807e52007-07-10 11:05:02 -0500559#endif
wdenk3861aa52002-09-27 23:19:37 +0000560
Jon Loeliger5336a762007-07-09 22:08:34 -0500561#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000562 *e++ = 1; /* Subnet mask request */
563 *e++ = 4;
564 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000565#endif
566
Jon Loeliger5336a762007-07-09 22:08:34 -0500567#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000568 *e++ = 3; /* Default gateway request */
569 *e++ = 4;
570 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000571#endif
572
Jon Loeliger5336a762007-07-09 22:08:34 -0500573#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000574 *e++ = 6; /* Domain Name Server */
575 *e++ = 4;
576 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000577#endif
578
Jon Loeliger5336a762007-07-09 22:08:34 -0500579#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000580 *e++ = 12; /* Host name request */
581 *e++ = 32;
582 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000583#endif
584
Jon Loeliger5336a762007-07-09 22:08:34 -0500585#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000586 *e++ = 13; /* Boot file size */
587 *e++ = 2;
588 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000589#endif
590
Jon Loeliger5336a762007-07-09 22:08:34 -0500591#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000592 *e++ = 17; /* Boot path */
593 *e++ = 32;
594 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000595#endif
596
Jon Loeliger5336a762007-07-09 22:08:34 -0500597#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000598 *e++ = 40; /* NIS Domain name request */
599 *e++ = 32;
600 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000601#endif
Luuk Paulussen6380e012011-05-16 18:29:19 +0000602#if defined(CONFIG_BOOTP_NTPSERVER)
603 *e++ = 42;
604 *e++ = 4;
605 e += 4;
606#endif
wdenk3861aa52002-09-27 23:19:37 +0000607
wdenk29e7f5a2004-03-12 00:14:09 +0000608 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000609
wdenk29e7f5a2004-03-12 00:14:09 +0000610 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000611}
Jon Loeligera9807e52007-07-10 11:05:02 -0500612#endif
wdenk3861aa52002-09-27 23:19:37 +0000613
614void
615BootpRequest (void)
616{
617 volatile uchar *pkt, *iphdr;
618 Bootp_t *bp;
619 int ext_len, pktlen, iplen;
620
Jon Loeliger54f35c22007-07-09 17:45:14 -0500621#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000622 dhcp_state = INIT;
623#endif
624
625#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
626 unsigned char bi_enetaddr[6];
627 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000628 ulong tst1, tst2, sum, m_mask, m_value = 0;
629
630 if (BootpTry ==0) {
631 /* get our mac */
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500632 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000633
Robin Getz9e0a4d62009-07-23 03:01:03 -0400634 debug("BootpRequest => Our Mac: ");
635 for (reg=0; reg<6; reg++)
636 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000637
638 /* Mac-Manipulation 2 get seed1 */
639 tst1=0;
640 tst2=0;
641 for (reg=2; reg<6; reg++) {
642 tst1 = tst1 << 8;
643 tst1 = tst1 | bi_enetaddr[reg];
644 }
645 for (reg=0; reg<2; reg++) {
646 tst2 = tst2 | bi_enetaddr[reg];
647 tst2 = tst2 << 8;
648 }
649
650 seed1 = tst1^tst2;
651
652 /* Mirror seed1*/
653 m_mask=0x1;
654 for (reg=1;reg<=32;reg++) {
655 m_value |= (m_mask & seed1);
656 seed1 = seed1 >> 1;
657 m_value = m_value << 1;
658 }
659 seed1 = m_value;
660 seed2 = 0xB78D0945;
661 }
662
663 /* Random Number Generator */
664
665 for (reg=0;reg<=0;reg++) {
666 sum = seed1 + seed2;
667 if (sum < seed1 || sum < seed2)
668 sum++;
wdenk57b2d802003-06-27 21:31:46 +0000669 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000670 seed1 = sum;
671
672 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
673 sum = sum >> (22-BootpTry);
674 } else { /*After 3rd BOOTP request max 8192 * 1ms */
675 sum = sum >> 19;
676 }
677 }
678
679 printf ("Random delay: %ld ms...\n", sum);
680 for (reg=0; reg <sum; reg++) {
681 udelay(1000); /*Wait 1ms*/
682 }
683#endif /* CONFIG_BOOTP_RANDOM_DELAY */
684
685 printf("BOOTP broadcast %d\n", ++BootpTry);
686 pkt = NetTxPacket;
687 memset ((void*)pkt, 0, PKTSIZE);
688
wdenk145d2c12004-04-15 21:48:45 +0000689 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000690
691 /*
692 * Next line results in incorrect packet size being transmitted, resulting
693 * in errors in some DHCP servers, reporting missing bytes. Size must be
694 * set in packet header after extension length has been determined.
695 * C. Hallinan, DS4.COM, Inc.
696 */
697 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
698 iphdr = pkt; /* We need this later for NetSetIP() */
699 pkt += IP_HDR_SIZE;
700
701 bp = (Bootp_t *)pkt;
702 bp->bp_op = OP_BOOTREQUEST;
703 bp->bp_htype = HWT_ETHER;
704 bp->bp_hlen = HWL_ETHER;
705 bp->bp_hops = 0;
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200706 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000707 NetWriteIP(&bp->bp_ciaddr, 0);
708 NetWriteIP(&bp->bp_yiaddr, 0);
709 NetWriteIP(&bp->bp_siaddr, 0);
710 NetWriteIP(&bp->bp_giaddr, 0);
711 memcpy (bp->bp_chaddr, NetOurEther, 6);
712 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
713
714 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500715#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200716 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000717#else
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200718 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeligera9807e52007-07-10 11:05:02 -0500719#endif
wdenk3861aa52002-09-27 23:19:37 +0000720
721 /*
722 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200723 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000724 */
725 BootpID = ((ulong)NetOurEther[2] << 24)
726 | ((ulong)NetOurEther[3] << 16)
727 | ((ulong)NetOurEther[4] << 8)
728 | (ulong)NetOurEther[5];
729 BootpID += get_timer(0);
wdenk29e7f5a2004-03-12 00:14:09 +0000730 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000731 NetCopyLong(&bp->bp_id, &BootpID);
732
733 /*
734 * Calculate proper packet lengths taking into account the
735 * variable size of the options field
736 */
Norbert van Bolhuis20a020c2009-06-04 09:39:48 +0200737 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000738 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
739 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200740 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000741
Jon Loeliger54f35c22007-07-09 17:45:14 -0500742#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000743 dhcp_state = SELECTING;
744 NetSetHandler(DhcpHandler);
745#else
746 NetSetHandler(BootpHandler);
Jon Loeligera9807e52007-07-10 11:05:02 -0500747#endif
wdenk3861aa52002-09-27 23:19:37 +0000748 NetSendPacket(NetTxPacket, pktlen);
749}
750
Jon Loeliger54f35c22007-07-09 17:45:14 -0500751#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk012429c2006-03-12 18:26:46 +0100752static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000753{
wdenkb02744a2003-04-05 00:53:31 +0000754 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000755 int oplen, size;
Wolfgang Denk2b184222009-09-11 09:05:32 +0200756#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
757 int *to_ptr;
758#endif
wdenk3861aa52002-09-27 23:19:37 +0000759
wdenk29e7f5a2004-03-12 00:14:09 +0000760 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000761 oplen = *(popt + 1);
wdenk29e7f5a2004-03-12 00:14:09 +0000762 switch (*popt) {
763 case 1:
764 NetCopyIP (&NetOurSubnetMask, (popt + 2));
765 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500766#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000767 case 2: /* Time offset */
Wolfgang Denk2b184222009-09-11 09:05:32 +0200768 to_ptr = &NetTimeOffset;
769 NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
wdenkb4ad9622005-04-01 00:25:43 +0000770 NetTimeOffset = ntohl (NetTimeOffset);
771 break;
772#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000773 case 3:
774 NetCopyIP (&NetOurGatewayIP, (popt + 2));
775 break;
776 case 6:
777 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500778#if defined(CONFIG_BOOTP_DNS2)
wdenk29e7f5a2004-03-12 00:14:09 +0000779 if (*(popt + 1) > 4) {
780 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
781 }
stroesee0aadfb2003-08-28 14:17:32 +0000782#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000783 break;
784 case 12:
785 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
786 memcpy (&NetOurHostName, popt + 2, size);
787 NetOurHostName[size] = 0;
788 break;
789 case 15: /* Ignore Domain Name Option */
790 break;
791 case 17:
792 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
793 memcpy (&NetOurRootPath, popt + 2, size);
794 NetOurRootPath[size] = 0;
795 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500796#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000797 case 42: /* NTP server IP */
798 NetCopyIP (&NetNtpServerIP, (popt + 2));
799 break;
800#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000801 case 51:
802 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
803 break;
804 case 53: /* Ignore Message Type Option */
805 break;
806 case 54:
807 NetCopyIP (&NetDHCPServerIP, (popt + 2));
808 break;
809 case 58: /* Ignore Renewal Time Option */
810 break;
811 case 59: /* Ignore Rebinding Time Option */
812 break;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100813 case 66: /* Ignore TFTP server name */
814 break;
815 case 67: /* vendor opt bootfile */
816 /*
817 * I can't use dhcp_vendorex_proc here because I need
818 * to write into the bootp packet - even then I had to
819 * pass the bootp packet pointer into here as the
820 * second arg
821 */
822 size = truncate_sz ("Opt Boot File",
823 sizeof(bp->bp_file),
824 oplen);
825 if (bp->bp_file[0] == '\0' && size > 0) {
826 /*
827 * only use vendor boot file if we didn't
828 * receive a boot file in the main non-vendor
829 * part of the packet - god only knows why
830 * some vendors chose not to use this perfectly
831 * good spot to store the boot file (join on
832 * Tru64 Unix) it seems mind bogglingly crazy
833 * to me
834 */
835 printf("*** WARNING: using vendor "
836 "optional boot file\n");
837 memcpy(bp->bp_file, popt + 2, size);
838 bp->bp_file[size] = '\0';
839 }
840 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000841 default:
Jon Loeliger5336a762007-07-09 22:08:34 -0500842#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000843 if (dhcp_vendorex_proc (popt))
wdenk57b2d802003-06-27 21:31:46 +0000844 break;
wdenk3861aa52002-09-27 23:19:37 +0000845#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000846 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
847 break;
wdenk3861aa52002-09-27 23:19:37 +0000848 }
849 popt += oplen + 2; /* Process next option */
850 }
851}
852
853static int DhcpMessageType(unsigned char *popt)
854{
855 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
856 return -1;
857
858 popt += 4;
859 while ( *popt != 0xff ) {
860 if ( *popt == 53 ) /* DHCP Message Type */
861 return *(popt + 2);
862 popt += *(popt + 1) + 2; /* Scan through all options */
863 }
864 return -1;
865}
866
wdenkb02744a2003-04-05 00:53:31 +0000867static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000868{
869 volatile uchar *pkt, *iphdr;
870 Bootp_t *bp;
871 int pktlen, iplen, extlen;
wdenkcc1e2562003-03-06 13:39:27 +0000872 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000873
Robin Getz9e0a4d62009-07-23 03:01:03 -0400874 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000875 pkt = NetTxPacket;
876 memset ((void*)pkt, 0, PKTSIZE);
877
wdenk145d2c12004-04-15 21:48:45 +0000878 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000879
880 iphdr = pkt; /* We'll need this later to set proper pkt size */
881 pkt += IP_HDR_SIZE;
882
883 bp = (Bootp_t *)pkt;
884 bp->bp_op = OP_BOOTREQUEST;
885 bp->bp_htype = HWT_ETHER;
886 bp->bp_hlen = HWL_ETHER;
887 bp->bp_hops = 0;
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200888 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammia01f256b2007-10-29 17:40:35 -0400889 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
890 * the server yet */
891
Wolfgang Denk8a933fb2006-10-12 00:01:08 +0200892 /*
Wolfgang Denk68e83a82006-10-09 01:26:14 +0200893 * RFC3046 requires Relay Agents to discard packets with
894 * nonzero and offered giaddr
895 */
896 NetWriteIP(&bp->bp_giaddr, 0);
897
wdenk3861aa52002-09-27 23:19:37 +0000898 memcpy (bp->bp_chaddr, NetOurEther, 6);
899
900 /*
901 * ID is the id of the OFFER packet
902 */
903
904 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
905
906 /*
907 * Copy options from OFFER packet if present
908 */
Justin Flammia01f256b2007-10-29 17:40:35 -0400909
910 /* Copy offered IP into the parameters request list */
911 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200912 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000913
Norbert van Bolhuis20a020c2009-06-04 09:39:48 +0200914 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000915 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
916 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
917
Robin Getz9e0a4d62009-07-23 03:01:03 -0400918 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichas72aa3f32008-03-26 09:43:57 +1100919#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
920 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
921#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000922 NetSendPacket(NetTxPacket, pktlen);
923}
924
925/*
926 * Handle DHCP received packets.
927 */
928static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000929DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
930 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000931{
932 Bootp_t *bp = (Bootp_t *)pkt;
933
Robin Getz9e0a4d62009-07-23 03:01:03 -0400934 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000935 src, dest, len, dhcp_state);
936
wdenk29e7f5a2004-03-12 00:14:09 +0000937 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000938 return;
939
Robin Getz9e0a4d62009-07-23 03:01:03 -0400940 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000941 src, dest, len, dhcp_state);
942
943 switch (dhcp_state) {
944 case SELECTING:
945 /*
946 * Wait an appropriate time for any potential DHCPOFFER packets
947 * to arrive. Then select one, and generate DHCPREQUEST response.
948 * If filename is in format we recognize, assume it is a valid
949 * OFFER from a server we want.
950 */
Robin Getz9e0a4d62009-07-23 03:01:03 -0400951 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200952#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000953 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200954 CONFIG_SYS_BOOTFILE_PREFIX,
955 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
956#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000957
Robin Getz9e0a4d62009-07-23 03:01:03 -0400958 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000959 dhcp_state = REQUESTING;
stroese5fa6e902003-04-10 13:26:44 +0000960
wdenk3861aa52002-09-27 23:19:37 +0000961 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk012429c2006-03-12 18:26:46 +0100962 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000963
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200964 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000965 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200966#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000967 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200968#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000969
970 return;
971 break;
972 case REQUESTING:
Robin Getz9e0a4d62009-07-23 03:01:03 -0400973 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000974
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200975 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000976 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk012429c2006-03-12 18:26:46 +0100977 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk29e7f5a2004-03-12 00:14:09 +0000978 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000979 dhcp_state = BOUND;
Mike Frysingerd5695f42009-02-17 00:00:53 -0500980 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
wdenk3861aa52002-09-27 23:19:37 +0000981
Simon Glassab068eb2011-06-13 16:13:12 -0700982 auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000983 return;
984 }
985 break;
Remy Bohmer03a44492008-08-20 11:30:28 +0200986 case BOUND:
987 /* DHCP client bound to address */
988 break;
wdenk3861aa52002-09-27 23:19:37 +0000989 default:
wdenk42c05472004-03-23 22:14:11 +0000990 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000991 break;
992 }
993
994}
995
996void DhcpRequest(void)
997{
998 BootpRequest();
999}
Wolfgang Denkf7a7f082007-11-03 23:09:27 +01001000#endif /* CONFIG_CMD_DHCP */