blob: e679f8b7f876e236bd7ec84b7218299c3bb82f57 [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"
15#include "tftp.h"
wdenk232c1502004-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
wdenk232c1502004-03-12 00:14:09 +000021#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000022
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020023#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-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
wdenk232c1502004-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 */
wdenk232c1502004-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 Loeliger643d1ab2007-07-09 17:45:14 -050043#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000044dhcp_state_t dhcp_state = INIT;
wdenk682011f2003-06-03 23:54:09 +000045unsigned long dhcp_leasetime = 0;
stroese759a51b2003-04-10 13:26:44 +000046IPaddr_t NetDHCPServerIP = 0;
wdenk3861aa52002-09-27 23:19:37 +000047static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len);
48
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
wdenk3861aa52002-09-27 23:19:37 +000065
Jon Loeliger1fe80d72007-07-09 22:08:34 -050066#if defined(CONFIG_BOOTP_VENDOREX)
wdenk3861aa52002-09-27 23:19:37 +000067extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
68extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
69#endif
70
Jon Loeliger610f2e92007-07-10 11:05:02 -050071#endif
wdenk3861aa52002-09-27 23:19:37 +000072
73static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
74{
75 Bootp_t *bp = (Bootp_t *) pkt;
76 int retval = 0;
77
78 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
79 retval = -1;
80 else if (len < sizeof (Bootp_t) - OPT_SIZE)
81 retval = -2;
82 else if (bp->bp_op != OP_BOOTREQUEST &&
83 bp->bp_op != OP_BOOTREPLY &&
84 bp->bp_op != DHCP_OFFER &&
85 bp->bp_op != DHCP_ACK &&
86 bp->bp_op != DHCP_NAK ) {
87 retval = -3;
88 }
89 else if (bp->bp_htype != HWT_ETHER)
90 retval = -4;
91 else if (bp->bp_hlen != HWL_ETHER)
92 retval = -5;
93 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
94 retval = -6;
95 }
96
Robin Getz0ebf04c2009-07-23 03:01:03 -040097 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000098
99 return retval;
100}
101
102/*
103 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
104 */
wdenk3e386912003-04-05 00:53:31 +0000105static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000106{
wdenk3d3befa2004-03-14 15:06:13 +0000107 IPaddr_t tmp_ip;
108
wdenk3861aa52002-09-27 23:19:37 +0000109 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400110#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000111 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
112 if (tmp_ip != 0)
113 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400114 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400115#endif
wdenk3d3befa2004-03-14 15:06:13 +0000116 if (strlen(bp->bp_file) > 0)
117 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000118
Robin Getz0ebf04c2009-07-23 03:01:03 -0400119 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000120
121 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000122 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000123 * not contain a new value
124 */
125 if (*BootFile) {
126 setenv ("bootfile", BootFile);
127 }
128}
129
130static int truncate_sz (const char *name, int maxlen, int curlen)
131{
132 if (curlen >= maxlen) {
133 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
134 name, curlen, maxlen);
135 curlen = maxlen - 1;
136 }
137 return (curlen);
138}
139
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500140#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000141
wdenk232c1502004-03-12 00:14:09 +0000142static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000143{
wdenk232c1502004-03-12 00:14:09 +0000144 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000145
Robin Getz0ebf04c2009-07-23 03:01:03 -0400146 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
wdenk232c1502004-03-12 00:14:09 +0000147 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000148
wdenk232c1502004-03-12 00:14:09 +0000149 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000150
wdenk232c1502004-03-12 00:14:09 +0000151 switch (*ext) {
152 /* Fixed length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700153 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000154 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000155 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000156 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700157 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000158 break;
wdenk232c1502004-03-12 00:14:09 +0000159 /* Variable length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700160 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000161 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000162 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000163 }
164 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700165 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000166 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700167 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000168 break;
169 case 6:
170 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000171 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000172 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500173#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000174 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000175 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000176 }
177#endif
wdenk3861aa52002-09-27 23:19:37 +0000178 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700179 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000180 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700181 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000182 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700183 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000184 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700185 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000186 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700187 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000188 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700189 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000190 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000191 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
192 memcpy (&NetOurHostName, ext + 2, size);
193 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000194 }
195 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700196 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000197 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000198 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000199 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000200 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000201 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700202 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000203 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700204 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000205 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700206 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000207 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700208 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000209 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000210 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
211 memcpy (&NetOurRootPath, ext + 2, size);
212 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000213 }
214 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700215 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000216 /*
wdenk8bde7f72003-06-27 21:31:46 +0000217 * This can be used to send the information of the
218 * vendor area in another file that the client can
219 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000220 */
221 break;
wdenk232c1502004-03-12 00:14:09 +0000222 /* IP host layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700223 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000224 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000225 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
226 memcpy (&NetOurNISDomain, ext + 2, size);
227 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000228 }
229 break;
wdenk232c1502004-03-12 00:14:09 +0000230 /* Application layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700231 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000232 /*
wdenk8bde7f72003-06-27 21:31:46 +0000233 * Binary information to exchange specific
234 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000235 */
236 break;
wdenk232c1502004-03-12 00:14:09 +0000237 /* Reserved (custom) fields (128..254) */
238 }
wdenk3861aa52002-09-27 23:19:37 +0000239}
240
wdenk232c1502004-03-12 00:14:09 +0000241static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000242{
wdenk232c1502004-03-12 00:14:09 +0000243 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000244
Robin Getz0ebf04c2009-07-23 03:01:03 -0400245 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000246
wdenk232c1502004-03-12 00:14:09 +0000247 while ((ext < end) && (*ext != 0xff)) {
248 if (*ext == 0) {
249 ext++;
250 } else {
251 u8 *opt = ext;
252
253 ext += ext[1] + 2;
254 if (ext <= end)
255 BootpVendorFieldProcess (opt);
256 }
wdenk3861aa52002-09-27 23:19:37 +0000257 }
wdenk3861aa52002-09-27 23:19:37 +0000258
Robin Getz0ebf04c2009-07-23 03:01:03 -0400259 debug("[BOOTP] Received fields: \n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500260 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400261 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000262
Mike Frysingerb6446b62009-02-17 00:00:53 -0500263 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400264 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000265
Robin Getz0ebf04c2009-07-23 03:01:03 -0400266 if (NetBootFileSize)
267 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000268
Robin Getz0ebf04c2009-07-23 03:01:03 -0400269 if (NetOurHostName[0])
270 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000271
Robin Getz0ebf04c2009-07-23 03:01:03 -0400272 if (NetOurRootPath[0])
273 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000274
Robin Getz0ebf04c2009-07-23 03:01:03 -0400275 if (NetOurNISDomain[0])
276 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000277
Robin Getz0ebf04c2009-07-23 03:01:03 -0400278 if (NetBootFileSize)
279 debug("NetBootFileSize: %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000280}
wdenk3861aa52002-09-27 23:19:37 +0000281/*
282 * Handle a BOOTP received packet.
283 */
284static void
285BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
286{
287 Bootp_t *bp;
288 char *s;
289
Robin Getz0ebf04c2009-07-23 03:01:03 -0400290 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000291 src, dest, len, sizeof (Bootp_t));
292
293 bp = (Bootp_t *)pkt;
294
wdenk232c1502004-03-12 00:14:09 +0000295 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000296 return;
297
298 /*
wdenk232c1502004-03-12 00:14:09 +0000299 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000300 */
301#ifdef CONFIG_STATUS_LED
302 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
303#endif
304
305 BootpCopyNetParams(bp); /* Store net parameters from reply */
306
307 /* Retrieve extended information (we must parse the vendor area) */
308 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200309 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000310
311 NetSetTimeout(0, (thand_f *)0);
312
Robin Getz0ebf04c2009-07-23 03:01:03 -0400313 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000314
wdenk132ba5f2004-02-27 08:20:54 +0000315 if ((s = getenv("autoload")) != NULL) {
316 if (*s == 'n') {
317 /*
318 * Just use BOOTP to configure system;
319 * Do not use TFTP to load the bootfile.
320 */
321 NetState = NETLOOP_SUCCESS;
322 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500323#if defined(CONFIG_CMD_NFS)
wdenk132ba5f2004-02-27 08:20:54 +0000324 } else if (strcmp(s, "NFS") == 0) {
325 /*
326 * Use NFS to load the bootfile.
327 */
328 NfsStart();
329 return;
wdenk0e6d7982004-03-14 00:07:33 +0000330#endif
wdenk132ba5f2004-02-27 08:20:54 +0000331 }
wdenk3861aa52002-09-27 23:19:37 +0000332 }
333
wdenk73a8b272003-06-05 19:27:42 +0000334 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000335}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500336#endif
wdenk3861aa52002-09-27 23:19:37 +0000337
338/*
339 * Timeout on BOOTP/DHCP request.
340 */
341static void
342BootpTimeout(void)
343{
344 if (BootpTry >= TIMEOUT_COUNT) {
345 puts ("\nRetry count exceeded; starting again\n");
346 NetStartAgain ();
347 } else {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200348 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000349 BootpRequest ();
350 }
351}
352
353/*
354 * Initialize BOOTP extension fields in the request.
355 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500356#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000357static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000358{
wdenk232c1502004-03-12 00:14:09 +0000359 u8 *start = e;
360 u8 *cnt;
361
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500362#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000363 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000364#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500365#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200366 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000367#endif
wdenk3861aa52002-09-27 23:19:37 +0000368
wdenk232c1502004-03-12 00:14:09 +0000369 *e++ = 99; /* RFC1048 Magic Cookie */
370 *e++ = 130;
371 *e++ = 83;
372 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000373
wdenk232c1502004-03-12 00:14:09 +0000374 *e++ = 53; /* DHCP Message Type */
375 *e++ = 1;
376 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000377
wdenk232c1502004-03-12 00:14:09 +0000378 *e++ = 57; /* Maximum DHCP Message Size */
379 *e++ = 2;
380 *e++ = (576 - 312 + OPT_SIZE) >> 8;
381 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000382
wdenk232c1502004-03-12 00:14:09 +0000383 if (ServerID) {
384 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000385
wdenk232c1502004-03-12 00:14:09 +0000386 *e++ = 54; /* ServerID */
387 *e++ = 4;
388 *e++ = tmp >> 24;
389 *e++ = tmp >> 16;
390 *e++ = tmp >> 8;
391 *e++ = tmp & 0xff;
392 }
wdenk3861aa52002-09-27 23:19:37 +0000393
wdenk232c1502004-03-12 00:14:09 +0000394 if (RequestedIP) {
395 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000396
wdenk232c1502004-03-12 00:14:09 +0000397 *e++ = 50; /* Requested IP */
398 *e++ = 4;
399 *e++ = tmp >> 24;
400 *e++ = tmp >> 16;
401 *e++ = tmp >> 8;
402 *e++ = tmp & 0xff;
403 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500404#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000405 if ((hostname = getenv ("hostname"))) {
406 int hostnamelen = strlen (hostname);
407
408 *e++ = 12; /* Hostname */
409 *e++ = hostnamelen;
410 memcpy (e, hostname, hostnamelen);
411 e += hostnamelen;
412 }
stroesefe389a82003-08-28 14:17:32 +0000413#endif
414
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500415#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000416 if ((x = dhcp_vendorex_prep (e)))
417 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000418#endif
419
wdenk232c1502004-03-12 00:14:09 +0000420 *e++ = 55; /* Parameter Request List */
421 cnt = e++; /* Pointer to count of requested items */
422 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500423#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000424 *e++ = 1; /* Subnet Mask */
425 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000426#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500427#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000428 *e++ = 2;
429 *cnt += 1;
430#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500431#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000432 *e++ = 3; /* Router Option */
433 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000434#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500435#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000436 *e++ = 6; /* DNS Server(s) */
437 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000438#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500439#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000440 *e++ = 12; /* Hostname */
441 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000442#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500443#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000444 *e++ = 13; /* Boot File Size */
445 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000446#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500447#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000448 *e++ = 17; /* Boot path */
449 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000450#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500451#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000452 *e++ = 40; /* NIS Domain name request */
453 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000454#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500455#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000456 *e++ = 42;
457 *cnt += 1;
458#endif
wdenk232c1502004-03-12 00:14:09 +0000459 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000460
wdenk232c1502004-03-12 00:14:09 +0000461 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000462#ifdef CONFIG_DHCP_MIN_EXT_LEN
wdenk232c1502004-03-12 00:14:09 +0000463 while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN)
464 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000465#endif
466
wdenk232c1502004-03-12 00:14:09 +0000467 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000468}
469
Jon Loeliger610f2e92007-07-10 11:05:02 -0500470#else
wdenk3861aa52002-09-27 23:19:37 +0000471/*
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500472 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000473 */
wdenk232c1502004-03-12 00:14:09 +0000474static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000475{
wdenk232c1502004-03-12 00:14:09 +0000476 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000477
wdenk232c1502004-03-12 00:14:09 +0000478 *e++ = 99; /* RFC1048 Magic Cookie */
479 *e++ = 130;
480 *e++ = 83;
481 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000482
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500483#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000484 *e++ = 53; /* DHCP Message Type */
485 *e++ = 1;
486 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000487
wdenk232c1502004-03-12 00:14:09 +0000488 *e++ = 57; /* Maximum DHCP Message Size */
489 *e++ = 2;
490 *e++ = (576 - 312 + OPT_SIZE) >> 16;
491 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500492#endif
wdenk3861aa52002-09-27 23:19:37 +0000493
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500494#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000495 *e++ = 1; /* Subnet mask request */
496 *e++ = 4;
497 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000498#endif
499
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500500#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000501 *e++ = 3; /* Default gateway request */
502 *e++ = 4;
503 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000504#endif
505
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500506#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000507 *e++ = 6; /* Domain Name Server */
508 *e++ = 4;
509 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000510#endif
511
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500512#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000513 *e++ = 12; /* Host name request */
514 *e++ = 32;
515 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000516#endif
517
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500518#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000519 *e++ = 13; /* Boot file size */
520 *e++ = 2;
521 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000522#endif
523
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500524#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000525 *e++ = 17; /* Boot path */
526 *e++ = 32;
527 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000528#endif
529
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500530#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000531 *e++ = 40; /* NIS Domain name request */
532 *e++ = 32;
533 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000534#endif
535
wdenk232c1502004-03-12 00:14:09 +0000536 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000537
wdenk232c1502004-03-12 00:14:09 +0000538 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000539}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500540#endif
wdenk3861aa52002-09-27 23:19:37 +0000541
542void
543BootpRequest (void)
544{
545 volatile uchar *pkt, *iphdr;
546 Bootp_t *bp;
547 int ext_len, pktlen, iplen;
548
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500549#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000550 dhcp_state = INIT;
551#endif
552
553#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
554 unsigned char bi_enetaddr[6];
555 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000556 ulong tst1, tst2, sum, m_mask, m_value = 0;
557
558 if (BootpTry ==0) {
559 /* get our mac */
Mike Frysinger95823ca2009-02-11 18:23:48 -0500560 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000561
Robin Getz0ebf04c2009-07-23 03:01:03 -0400562 debug("BootpRequest => Our Mac: ");
563 for (reg=0; reg<6; reg++)
564 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000565
566 /* Mac-Manipulation 2 get seed1 */
567 tst1=0;
568 tst2=0;
569 for (reg=2; reg<6; reg++) {
570 tst1 = tst1 << 8;
571 tst1 = tst1 | bi_enetaddr[reg];
572 }
573 for (reg=0; reg<2; reg++) {
574 tst2 = tst2 | bi_enetaddr[reg];
575 tst2 = tst2 << 8;
576 }
577
578 seed1 = tst1^tst2;
579
580 /* Mirror seed1*/
581 m_mask=0x1;
582 for (reg=1;reg<=32;reg++) {
583 m_value |= (m_mask & seed1);
584 seed1 = seed1 >> 1;
585 m_value = m_value << 1;
586 }
587 seed1 = m_value;
588 seed2 = 0xB78D0945;
589 }
590
591 /* Random Number Generator */
592
593 for (reg=0;reg<=0;reg++) {
594 sum = seed1 + seed2;
595 if (sum < seed1 || sum < seed2)
596 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000597 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000598 seed1 = sum;
599
600 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
601 sum = sum >> (22-BootpTry);
602 } else { /*After 3rd BOOTP request max 8192 * 1ms */
603 sum = sum >> 19;
604 }
605 }
606
607 printf ("Random delay: %ld ms...\n", sum);
608 for (reg=0; reg <sum; reg++) {
609 udelay(1000); /*Wait 1ms*/
610 }
611#endif /* CONFIG_BOOTP_RANDOM_DELAY */
612
613 printf("BOOTP broadcast %d\n", ++BootpTry);
614 pkt = NetTxPacket;
615 memset ((void*)pkt, 0, PKTSIZE);
616
wdenka3d991b2004-04-15 21:48:45 +0000617 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000618
619 /*
620 * Next line results in incorrect packet size being transmitted, resulting
621 * in errors in some DHCP servers, reporting missing bytes. Size must be
622 * set in packet header after extension length has been determined.
623 * C. Hallinan, DS4.COM, Inc.
624 */
625 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
626 iphdr = pkt; /* We need this later for NetSetIP() */
627 pkt += IP_HDR_SIZE;
628
629 bp = (Bootp_t *)pkt;
630 bp->bp_op = OP_BOOTREQUEST;
631 bp->bp_htype = HWT_ETHER;
632 bp->bp_hlen = HWL_ETHER;
633 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200634 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000635 NetWriteIP(&bp->bp_ciaddr, 0);
636 NetWriteIP(&bp->bp_yiaddr, 0);
637 NetWriteIP(&bp->bp_siaddr, 0);
638 NetWriteIP(&bp->bp_giaddr, 0);
639 memcpy (bp->bp_chaddr, NetOurEther, 6);
640 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
641
642 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500643#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200644 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000645#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200646 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500647#endif
wdenk3861aa52002-09-27 23:19:37 +0000648
649 /*
650 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200651 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000652 */
653 BootpID = ((ulong)NetOurEther[2] << 24)
654 | ((ulong)NetOurEther[3] << 16)
655 | ((ulong)NetOurEther[4] << 8)
656 | (ulong)NetOurEther[5];
657 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000658 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000659 NetCopyLong(&bp->bp_id, &BootpID);
660
661 /*
662 * Calculate proper packet lengths taking into account the
663 * variable size of the options field
664 */
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200665 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000666 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
667 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200668 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000669
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500670#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000671 dhcp_state = SELECTING;
672 NetSetHandler(DhcpHandler);
673#else
674 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500675#endif
wdenk3861aa52002-09-27 23:19:37 +0000676 NetSendPacket(NetTxPacket, pktlen);
677}
678
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500679#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100680static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000681{
wdenk3e386912003-04-05 00:53:31 +0000682 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000683 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200684#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
685 int *to_ptr;
686#endif
wdenk3861aa52002-09-27 23:19:37 +0000687
wdenk232c1502004-03-12 00:14:09 +0000688 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000689 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000690 switch (*popt) {
691 case 1:
692 NetCopyIP (&NetOurSubnetMask, (popt + 2));
693 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500694#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000695 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200696 to_ptr = &NetTimeOffset;
697 NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000698 NetTimeOffset = ntohl (NetTimeOffset);
699 break;
700#endif
wdenk232c1502004-03-12 00:14:09 +0000701 case 3:
702 NetCopyIP (&NetOurGatewayIP, (popt + 2));
703 break;
704 case 6:
705 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500706#if defined(CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000707 if (*(popt + 1) > 4) {
708 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
709 }
stroesefe389a82003-08-28 14:17:32 +0000710#endif
wdenk232c1502004-03-12 00:14:09 +0000711 break;
712 case 12:
713 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
714 memcpy (&NetOurHostName, popt + 2, size);
715 NetOurHostName[size] = 0;
716 break;
717 case 15: /* Ignore Domain Name Option */
718 break;
719 case 17:
720 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
721 memcpy (&NetOurRootPath, popt + 2, size);
722 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 */
726 NetCopyIP (&NetNtpServerIP, (popt + 2));
727 break;
728#endif
wdenk232c1502004-03-12 00:14:09 +0000729 case 51:
730 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
731 break;
732 case 53: /* Ignore Message Type Option */
733 break;
734 case 54:
735 NetCopyIP (&NetDHCPServerIP, (popt + 2));
736 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 */
750 size = truncate_sz ("Opt Boot File",
751 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)
wdenk232c1502004-03-12 00:14:09 +0000771 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000772 break;
wdenk3861aa52002-09-27 23:19:37 +0000773#endif
wdenk232c1502004-03-12 00:14:09 +0000774 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
775 break;
wdenk3861aa52002-09-27 23:19:37 +0000776 }
777 popt += oplen + 2; /* Process next option */
778 }
779}
780
781static int DhcpMessageType(unsigned char *popt)
782{
783 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
784 return -1;
785
786 popt += 4;
787 while ( *popt != 0xff ) {
788 if ( *popt == 53 ) /* DHCP Message Type */
789 return *(popt + 2);
790 popt += *(popt + 1) + 2; /* Scan through all options */
791 }
792 return -1;
793}
794
wdenk3e386912003-04-05 00:53:31 +0000795static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000796{
797 volatile uchar *pkt, *iphdr;
798 Bootp_t *bp;
799 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000800 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000801
Robin Getz0ebf04c2009-07-23 03:01:03 -0400802 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000803 pkt = NetTxPacket;
804 memset ((void*)pkt, 0, PKTSIZE);
805
wdenka3d991b2004-04-15 21:48:45 +0000806 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000807
808 iphdr = pkt; /* We'll need this later to set proper pkt size */
809 pkt += IP_HDR_SIZE;
810
811 bp = (Bootp_t *)pkt;
812 bp->bp_op = OP_BOOTREQUEST;
813 bp->bp_htype = HWT_ETHER;
814 bp->bp_hlen = HWL_ETHER;
815 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200816 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammiae5c794e2007-10-29 17:40:35 -0400817 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
818 * the server yet */
819
Wolfgang Denkc6686702006-10-12 00:01:08 +0200820 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200821 * RFC3046 requires Relay Agents to discard packets with
822 * nonzero and offered giaddr
823 */
824 NetWriteIP(&bp->bp_giaddr, 0);
825
wdenk3861aa52002-09-27 23:19:37 +0000826 memcpy (bp->bp_chaddr, NetOurEther, 6);
827
828 /*
829 * ID is the id of the OFFER packet
830 */
831
832 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
833
834 /*
835 * Copy options from OFFER packet if present
836 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400837
838 /* Copy offered IP into the parameters request list */
839 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200840 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000841
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200842 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000843 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
844 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
845
Robin Getz0ebf04c2009-07-23 03:01:03 -0400846 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100847#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
848 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
849#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000850 NetSendPacket(NetTxPacket, pktlen);
851}
852
853/*
854 * Handle DHCP received packets.
855 */
856static void
857DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
858{
859 Bootp_t *bp = (Bootp_t *)pkt;
860
Robin Getz0ebf04c2009-07-23 03:01:03 -0400861 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000862 src, dest, len, dhcp_state);
863
wdenk232c1502004-03-12 00:14:09 +0000864 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000865 return;
866
Robin Getz0ebf04c2009-07-23 03:01:03 -0400867 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000868 src, dest, len, dhcp_state);
869
870 switch (dhcp_state) {
871 case SELECTING:
872 /*
873 * Wait an appropriate time for any potential DHCPOFFER packets
874 * to arrive. Then select one, and generate DHCPREQUEST response.
875 * If filename is in format we recognize, assume it is a valid
876 * OFFER from a server we want.
877 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400878 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200879#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000880 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200881 CONFIG_SYS_BOOTFILE_PREFIX,
882 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
883#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000884
Robin Getz0ebf04c2009-07-23 03:01:03 -0400885 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000886 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000887
wdenk3861aa52002-09-27 23:19:37 +0000888 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100889 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000890
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200891 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000892 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200893#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000894 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200895#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000896
897 return;
898 break;
899 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400900 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000901
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200902 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000903 char *s;
904
905 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100906 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk232c1502004-03-12 00:14:09 +0000907 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000908 dhcp_state = BOUND;
Mike Frysingerb6446b62009-02-17 00:00:53 -0500909 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
wdenk3861aa52002-09-27 23:19:37 +0000910
911 /* Obey the 'autoload' setting */
wdenk132ba5f2004-02-27 08:20:54 +0000912 if ((s = getenv("autoload")) != NULL) {
913 if (*s == 'n') {
914 /*
915 * Just use BOOTP to configure system;
916 * Do not use TFTP to load the bootfile.
917 */
918 NetState = NETLOOP_SUCCESS;
919 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500920#if defined(CONFIG_CMD_NFS)
wdenk132ba5f2004-02-27 08:20:54 +0000921 } else if (strcmp(s, "NFS") == 0) {
922 /*
923 * Use NFS to load the bootfile.
924 */
925 NfsStart();
926 return;
wdenk0e6d7982004-03-14 00:07:33 +0000927#endif
wdenk132ba5f2004-02-27 08:20:54 +0000928 }
wdenk3861aa52002-09-27 23:19:37 +0000929 }
wdenk73a8b272003-06-05 19:27:42 +0000930 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000931 return;
932 }
933 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200934 case BOUND:
935 /* DHCP client bound to address */
936 break;
wdenk3861aa52002-09-27 23:19:37 +0000937 default:
wdenk4b9206e2004-03-23 22:14:11 +0000938 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000939 break;
940 }
941
942}
943
944void DhcpRequest(void)
945{
946 BootpRequest();
947}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100948#endif /* CONFIG_CMD_DHCP */