blob: d1cbd39c2b032743b9d86338b12012eaa3e881bb [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
11#if 0
wdenk232c1502004-03-12 00:14:09 +000012#define DEBUG 1 /* general debug */
13#define DEBUG_BOOTP_EXT 1 /* Debug received vendor fields */
wdenk3861aa52002-09-27 23:19:37 +000014#endif
15
16#ifdef DEBUG_BOOTP_EXT
17#define debug_ext(fmt,args...) printf (fmt ,##args)
18#else
19#define debug_ext(fmt,args...)
20#endif
21
22#include <common.h>
23#include <command.h>
24#include <net.h>
25#include "bootp.h"
26#include "tftp.h"
wdenk232c1502004-03-12 00:14:09 +000027#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000028#ifdef CONFIG_STATUS_LED
29#include <status_led.h>
30#endif
31
wdenk232c1502004-03-12 00:14:09 +000032#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000033
Jon Loeliger643d1ab2007-07-09 17:45:14 -050034#if defined(CONFIG_CMD_NET)
wdenk3861aa52002-09-27 23:19:37 +000035
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020036#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000037#ifndef CONFIG_NET_RETRY_COUNT
wdenk3861aa52002-09-27 23:19:37 +000038# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
39#else
wdenk232c1502004-03-12 00:14:09 +000040# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000041#endif
42
43#define PORT_BOOTPS 67 /* BOOTP server UDP port */
44#define PORT_BOOTPC 68 /* BOOTP client UDP port */
45
46#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000047#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000048#endif
49
50ulong BootpID;
51int BootpTry;
52#ifdef CONFIG_BOOTP_RANDOM_DELAY
53ulong seed1, seed2;
54#endif
55
Jon Loeliger643d1ab2007-07-09 17:45:14 -050056#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000057dhcp_state_t dhcp_state = INIT;
wdenk682011f2003-06-03 23:54:09 +000058unsigned long dhcp_leasetime = 0;
stroese759a51b2003-04-10 13:26:44 +000059IPaddr_t NetDHCPServerIP = 0;
wdenk3861aa52002-09-27 23:19:37 +000060static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len);
61
62/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000063#if 0
64static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000065{
66 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000067 case 1: return "DHCPDISCOVER"; break;
68 case 2: return "DHCPOFFER"; break;
69 case 3: return "DHCPREQUEST"; break;
70 case 4: return "DHCPDECLINE"; break;
71 case 5: return "DHCPACK"; break;
72 case 6: return "DHCPNACK"; break;
73 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000074 default: return "UNKNOWN/INVALID MSG TYPE"; break;
75 }
76}
wdenk3e386912003-04-05 00:53:31 +000077#endif
wdenk3861aa52002-09-27 23:19:37 +000078
Jon Loeliger1fe80d72007-07-09 22:08:34 -050079#if defined(CONFIG_BOOTP_VENDOREX)
wdenk3861aa52002-09-27 23:19:37 +000080extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
81extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
82#endif
83
Jon Loeliger610f2e92007-07-10 11:05:02 -050084#endif
wdenk3861aa52002-09-27 23:19:37 +000085
86static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
87{
88 Bootp_t *bp = (Bootp_t *) pkt;
89 int retval = 0;
90
91 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
92 retval = -1;
93 else if (len < sizeof (Bootp_t) - OPT_SIZE)
94 retval = -2;
95 else if (bp->bp_op != OP_BOOTREQUEST &&
96 bp->bp_op != OP_BOOTREPLY &&
97 bp->bp_op != DHCP_OFFER &&
98 bp->bp_op != DHCP_ACK &&
99 bp->bp_op != DHCP_NAK ) {
100 retval = -3;
101 }
102 else if (bp->bp_htype != HWT_ETHER)
103 retval = -4;
104 else if (bp->bp_hlen != HWL_ETHER)
105 retval = -5;
106 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
107 retval = -6;
108 }
109
110 debug ("Filtering pkt = %d\n", retval);
111
112 return retval;
113}
114
115/*
116 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
117 */
wdenk3e386912003-04-05 00:53:31 +0000118static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000119{
wdenk3d3befa2004-03-14 15:06:13 +0000120 IPaddr_t tmp_ip;
121
wdenk3861aa52002-09-27 23:19:37 +0000122 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400123#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000124 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
125 if (tmp_ip != 0)
126 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
wdenk3861aa52002-09-27 23:19:37 +0000127 memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400128#endif
wdenk3d3befa2004-03-14 15:06:13 +0000129 if (strlen(bp->bp_file) > 0)
130 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000131
132 debug ("Bootfile: %s\n", BootFile);
133
134 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000135 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000136 * not contain a new value
137 */
138 if (*BootFile) {
139 setenv ("bootfile", BootFile);
140 }
141}
142
143static int truncate_sz (const char *name, int maxlen, int curlen)
144{
145 if (curlen >= maxlen) {
146 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
147 name, curlen, maxlen);
148 curlen = maxlen - 1;
149 }
150 return (curlen);
151}
152
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500153#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000154
wdenk232c1502004-03-12 00:14:09 +0000155static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000156{
wdenk232c1502004-03-12 00:14:09 +0000157 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000158
wdenk232c1502004-03-12 00:14:09 +0000159 debug_ext ("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
160 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000161
wdenk232c1502004-03-12 00:14:09 +0000162 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000163
wdenk232c1502004-03-12 00:14:09 +0000164 switch (*ext) {
165 /* Fixed length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700166 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000167 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000168 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000169 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700170 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000171 break;
wdenk232c1502004-03-12 00:14:09 +0000172 /* Variable length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700173 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000174 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000175 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000176 }
177 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700178 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000179 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700180 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000181 break;
182 case 6:
183 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000184 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000185 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500186#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000187 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000188 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000189 }
190#endif
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700192 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000193 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700194 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000195 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700196 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000197 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700198 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000199 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700200 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000201 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700202 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000203 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000204 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
205 memcpy (&NetOurHostName, ext + 2, size);
206 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000207 }
208 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700209 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000210 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000211 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000212 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000213 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000214 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700215 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000216 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700217 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000218 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700219 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000220 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700221 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000222 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000223 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
224 memcpy (&NetOurRootPath, ext + 2, size);
225 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000226 }
227 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700228 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 /*
wdenk8bde7f72003-06-27 21:31:46 +0000230 * This can be used to send the information of the
231 * vendor area in another file that the client can
232 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000233 */
234 break;
wdenk232c1502004-03-12 00:14:09 +0000235 /* IP host layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700236 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000237 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000238 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
239 memcpy (&NetOurNISDomain, ext + 2, size);
240 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000241 }
242 break;
wdenk232c1502004-03-12 00:14:09 +0000243 /* Application layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700244 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000245 /*
wdenk8bde7f72003-06-27 21:31:46 +0000246 * Binary information to exchange specific
247 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000248 */
249 break;
wdenk232c1502004-03-12 00:14:09 +0000250 /* Reserved (custom) fields (128..254) */
251 }
wdenk3861aa52002-09-27 23:19:37 +0000252}
253
wdenk232c1502004-03-12 00:14:09 +0000254static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000255{
wdenk232c1502004-03-12 00:14:09 +0000256 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000257
wdenk232c1502004-03-12 00:14:09 +0000258 debug_ext ("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000259
wdenk232c1502004-03-12 00:14:09 +0000260 while ((ext < end) && (*ext != 0xff)) {
261 if (*ext == 0) {
262 ext++;
263 } else {
264 u8 *opt = ext;
265
266 ext += ext[1] + 2;
267 if (ext <= end)
268 BootpVendorFieldProcess (opt);
269 }
wdenk3861aa52002-09-27 23:19:37 +0000270 }
wdenk3861aa52002-09-27 23:19:37 +0000271
272#ifdef DEBUG_BOOTP_EXT
wdenk4b9206e2004-03-23 22:14:11 +0000273 puts ("[BOOTP] Received fields: \n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500274 if (NetOurSubnetMask)
275 printf ("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000276
Mike Frysingerb6446b62009-02-17 00:00:53 -0500277 if (NetOurGatewayIP)
278 printf ("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000279
wdenk232c1502004-03-12 00:14:09 +0000280 if (NetBootFileSize) {
281 printf ("NetBootFileSize : %d\n", NetBootFileSize);
282 }
wdenk3861aa52002-09-27 23:19:37 +0000283
wdenk232c1502004-03-12 00:14:09 +0000284 if (NetOurHostName[0]) {
285 printf ("NetOurHostName : %s\n", NetOurHostName);
286 }
wdenk3861aa52002-09-27 23:19:37 +0000287
wdenk232c1502004-03-12 00:14:09 +0000288 if (NetOurRootPath[0]) {
289 printf ("NetOurRootPath : %s\n", NetOurRootPath);
290 }
wdenk3861aa52002-09-27 23:19:37 +0000291
wdenk232c1502004-03-12 00:14:09 +0000292 if (NetOurNISDomain[0]) {
293 printf ("NetOurNISDomain : %s\n", NetOurNISDomain);
294 }
wdenk3861aa52002-09-27 23:19:37 +0000295
wdenk232c1502004-03-12 00:14:09 +0000296 if (NetBootFileSize) {
297 printf ("NetBootFileSize: %d\n", NetBootFileSize);
298 }
299#endif /* DEBUG_BOOTP_EXT */
wdenk3861aa52002-09-27 23:19:37 +0000300}
wdenk3861aa52002-09-27 23:19:37 +0000301/*
302 * Handle a BOOTP received packet.
303 */
304static void
305BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
306{
307 Bootp_t *bp;
308 char *s;
309
Wolfgang Denkb64f1902008-07-14 15:06:35 +0200310 debug ("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000311 src, dest, len, sizeof (Bootp_t));
312
313 bp = (Bootp_t *)pkt;
314
wdenk232c1502004-03-12 00:14:09 +0000315 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000316 return;
317
318 /*
wdenk232c1502004-03-12 00:14:09 +0000319 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000320 */
321#ifdef CONFIG_STATUS_LED
322 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
323#endif
324
325 BootpCopyNetParams(bp); /* Store net parameters from reply */
326
327 /* Retrieve extended information (we must parse the vendor area) */
328 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200329 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000330
331 NetSetTimeout(0, (thand_f *)0);
332
333 debug ("Got good BOOTP\n");
334
wdenk132ba5f2004-02-27 08:20:54 +0000335 if ((s = getenv("autoload")) != NULL) {
336 if (*s == 'n') {
337 /*
338 * Just use BOOTP to configure system;
339 * Do not use TFTP to load the bootfile.
340 */
341 NetState = NETLOOP_SUCCESS;
342 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500343#if defined(CONFIG_CMD_NFS)
wdenk132ba5f2004-02-27 08:20:54 +0000344 } else if (strcmp(s, "NFS") == 0) {
345 /*
346 * Use NFS to load the bootfile.
347 */
348 NfsStart();
349 return;
wdenk0e6d7982004-03-14 00:07:33 +0000350#endif
wdenk132ba5f2004-02-27 08:20:54 +0000351 }
wdenk3861aa52002-09-27 23:19:37 +0000352 }
353
wdenk73a8b272003-06-05 19:27:42 +0000354 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000355}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500356#endif
wdenk3861aa52002-09-27 23:19:37 +0000357
358/*
359 * Timeout on BOOTP/DHCP request.
360 */
361static void
362BootpTimeout(void)
363{
364 if (BootpTry >= TIMEOUT_COUNT) {
365 puts ("\nRetry count exceeded; starting again\n");
366 NetStartAgain ();
367 } else {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200368 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000369 BootpRequest ();
370 }
371}
372
373/*
374 * Initialize BOOTP extension fields in the request.
375 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500376#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000377static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000378{
wdenk232c1502004-03-12 00:14:09 +0000379 u8 *start = e;
380 u8 *cnt;
381
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500382#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000383 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000384#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500385#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200386 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000387#endif
wdenk3861aa52002-09-27 23:19:37 +0000388
wdenk232c1502004-03-12 00:14:09 +0000389 *e++ = 99; /* RFC1048 Magic Cookie */
390 *e++ = 130;
391 *e++ = 83;
392 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000393
wdenk232c1502004-03-12 00:14:09 +0000394 *e++ = 53; /* DHCP Message Type */
395 *e++ = 1;
396 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000397
wdenk232c1502004-03-12 00:14:09 +0000398 *e++ = 57; /* Maximum DHCP Message Size */
399 *e++ = 2;
400 *e++ = (576 - 312 + OPT_SIZE) >> 8;
401 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000402
wdenk232c1502004-03-12 00:14:09 +0000403 if (ServerID) {
404 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000405
wdenk232c1502004-03-12 00:14:09 +0000406 *e++ = 54; /* ServerID */
407 *e++ = 4;
408 *e++ = tmp >> 24;
409 *e++ = tmp >> 16;
410 *e++ = tmp >> 8;
411 *e++ = tmp & 0xff;
412 }
wdenk3861aa52002-09-27 23:19:37 +0000413
wdenk232c1502004-03-12 00:14:09 +0000414 if (RequestedIP) {
415 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000416
wdenk232c1502004-03-12 00:14:09 +0000417 *e++ = 50; /* Requested IP */
418 *e++ = 4;
419 *e++ = tmp >> 24;
420 *e++ = tmp >> 16;
421 *e++ = tmp >> 8;
422 *e++ = tmp & 0xff;
423 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500424#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000425 if ((hostname = getenv ("hostname"))) {
426 int hostnamelen = strlen (hostname);
427
428 *e++ = 12; /* Hostname */
429 *e++ = hostnamelen;
430 memcpy (e, hostname, hostnamelen);
431 e += hostnamelen;
432 }
stroesefe389a82003-08-28 14:17:32 +0000433#endif
434
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500435#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000436 if ((x = dhcp_vendorex_prep (e)))
437 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000438#endif
439
wdenk232c1502004-03-12 00:14:09 +0000440 *e++ = 55; /* Parameter Request List */
441 cnt = e++; /* Pointer to count of requested items */
442 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500443#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000444 *e++ = 1; /* Subnet Mask */
445 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000446#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500447#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000448 *e++ = 2;
449 *cnt += 1;
450#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500451#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000452 *e++ = 3; /* Router Option */
453 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000454#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500455#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000456 *e++ = 6; /* DNS Server(s) */
457 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000458#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500459#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000460 *e++ = 12; /* Hostname */
461 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000462#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500463#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 13; /* Boot File Size */
465 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000466#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000468 *e++ = 17; /* Boot path */
469 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000470#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500471#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 40; /* NIS Domain name request */
473 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500475#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000476 *e++ = 42;
477 *cnt += 1;
478#endif
wdenk232c1502004-03-12 00:14:09 +0000479 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000480
wdenk232c1502004-03-12 00:14:09 +0000481 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000482#ifdef CONFIG_DHCP_MIN_EXT_LEN
wdenk232c1502004-03-12 00:14:09 +0000483 while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN)
484 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000485#endif
486
wdenk232c1502004-03-12 00:14:09 +0000487 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000488}
489
Jon Loeliger610f2e92007-07-10 11:05:02 -0500490#else
wdenk3861aa52002-09-27 23:19:37 +0000491/*
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500492 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000493 */
wdenk232c1502004-03-12 00:14:09 +0000494static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000495{
wdenk232c1502004-03-12 00:14:09 +0000496 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000497
wdenk232c1502004-03-12 00:14:09 +0000498 *e++ = 99; /* RFC1048 Magic Cookie */
499 *e++ = 130;
500 *e++ = 83;
501 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000502
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500503#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000504 *e++ = 53; /* DHCP Message Type */
505 *e++ = 1;
506 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000507
wdenk232c1502004-03-12 00:14:09 +0000508 *e++ = 57; /* Maximum DHCP Message Size */
509 *e++ = 2;
510 *e++ = (576 - 312 + OPT_SIZE) >> 16;
511 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500512#endif
wdenk3861aa52002-09-27 23:19:37 +0000513
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500514#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000515 *e++ = 1; /* Subnet mask request */
516 *e++ = 4;
517 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000518#endif
519
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500520#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000521 *e++ = 3; /* Default gateway request */
522 *e++ = 4;
523 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000524#endif
525
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500526#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000527 *e++ = 6; /* Domain Name Server */
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_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000533 *e++ = 12; /* Host name request */
534 *e++ = 32;
535 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
537
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500538#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000539 *e++ = 13; /* Boot file size */
540 *e++ = 2;
541 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000542#endif
543
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500544#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000545 *e++ = 17; /* Boot path */
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_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000551 *e++ = 40; /* NIS Domain name request */
552 *e++ = 32;
553 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000554#endif
555
wdenk232c1502004-03-12 00:14:09 +0000556 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000557
wdenk232c1502004-03-12 00:14:09 +0000558 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000559}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500560#endif
wdenk3861aa52002-09-27 23:19:37 +0000561
562void
563BootpRequest (void)
564{
565 volatile uchar *pkt, *iphdr;
566 Bootp_t *bp;
567 int ext_len, pktlen, iplen;
568
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500569#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000570 dhcp_state = INIT;
571#endif
572
573#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
574 unsigned char bi_enetaddr[6];
575 int reg;
576 char *e,*s;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200577 char tmp[64];
wdenk3861aa52002-09-27 23:19:37 +0000578 ulong tst1, tst2, sum, m_mask, m_value = 0;
579
580 if (BootpTry ==0) {
581 /* get our mac */
582 reg = getenv_r ("ethaddr", tmp, sizeof(tmp));
583 s = (reg > 0) ? tmp : NULL;
584
585 for (reg=0; reg<6; ++reg) {
586 bi_enetaddr[reg] = s ? simple_strtoul(s, &e, 16) : 0;
587 if (s) {
588 s = (*e) ? e+1 : e;
589 }
590 }
591#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000592 puts ("BootpRequest => Our Mac: ");
wdenk3861aa52002-09-27 23:19:37 +0000593 for (reg=0; reg<6; reg++) {
594 printf ("%x%c",
595 bi_enetaddr[reg],
596 reg==5 ? '\n' : ':');
597 }
598#endif /* DEBUG */
599
600 /* Mac-Manipulation 2 get seed1 */
601 tst1=0;
602 tst2=0;
603 for (reg=2; reg<6; reg++) {
604 tst1 = tst1 << 8;
605 tst1 = tst1 | bi_enetaddr[reg];
606 }
607 for (reg=0; reg<2; reg++) {
608 tst2 = tst2 | bi_enetaddr[reg];
609 tst2 = tst2 << 8;
610 }
611
612 seed1 = tst1^tst2;
613
614 /* Mirror seed1*/
615 m_mask=0x1;
616 for (reg=1;reg<=32;reg++) {
617 m_value |= (m_mask & seed1);
618 seed1 = seed1 >> 1;
619 m_value = m_value << 1;
620 }
621 seed1 = m_value;
622 seed2 = 0xB78D0945;
623 }
624
625 /* Random Number Generator */
626
627 for (reg=0;reg<=0;reg++) {
628 sum = seed1 + seed2;
629 if (sum < seed1 || sum < seed2)
630 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000631 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000632 seed1 = sum;
633
634 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
635 sum = sum >> (22-BootpTry);
636 } else { /*After 3rd BOOTP request max 8192 * 1ms */
637 sum = sum >> 19;
638 }
639 }
640
641 printf ("Random delay: %ld ms...\n", sum);
642 for (reg=0; reg <sum; reg++) {
643 udelay(1000); /*Wait 1ms*/
644 }
645#endif /* CONFIG_BOOTP_RANDOM_DELAY */
646
647 printf("BOOTP broadcast %d\n", ++BootpTry);
648 pkt = NetTxPacket;
649 memset ((void*)pkt, 0, PKTSIZE);
650
wdenka3d991b2004-04-15 21:48:45 +0000651 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000652
653 /*
654 * Next line results in incorrect packet size being transmitted, resulting
655 * in errors in some DHCP servers, reporting missing bytes. Size must be
656 * set in packet header after extension length has been determined.
657 * C. Hallinan, DS4.COM, Inc.
658 */
659 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
660 iphdr = pkt; /* We need this later for NetSetIP() */
661 pkt += IP_HDR_SIZE;
662
663 bp = (Bootp_t *)pkt;
664 bp->bp_op = OP_BOOTREQUEST;
665 bp->bp_htype = HWT_ETHER;
666 bp->bp_hlen = HWL_ETHER;
667 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200668 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000669 NetWriteIP(&bp->bp_ciaddr, 0);
670 NetWriteIP(&bp->bp_yiaddr, 0);
671 NetWriteIP(&bp->bp_siaddr, 0);
672 NetWriteIP(&bp->bp_giaddr, 0);
673 memcpy (bp->bp_chaddr, NetOurEther, 6);
674 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
675
676 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500677#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200678 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000679#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200680 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500681#endif
wdenk3861aa52002-09-27 23:19:37 +0000682
683 /*
684 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200685 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000686 */
687 BootpID = ((ulong)NetOurEther[2] << 24)
688 | ((ulong)NetOurEther[3] << 16)
689 | ((ulong)NetOurEther[4] << 8)
690 | (ulong)NetOurEther[5];
691 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000692 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000693 NetCopyLong(&bp->bp_id, &BootpID);
694
695 /*
696 * Calculate proper packet lengths taking into account the
697 * variable size of the options field
698 */
699 pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + ext_len;
700 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
701 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200702 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000703
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500704#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000705 dhcp_state = SELECTING;
706 NetSetHandler(DhcpHandler);
707#else
708 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500709#endif
wdenk3861aa52002-09-27 23:19:37 +0000710 NetSendPacket(NetTxPacket, pktlen);
711}
712
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500713#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100714static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000715{
wdenk3e386912003-04-05 00:53:31 +0000716 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000717 int oplen, size;
718
wdenk232c1502004-03-12 00:14:09 +0000719 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000720 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000721 switch (*popt) {
722 case 1:
723 NetCopyIP (&NetOurSubnetMask, (popt + 2));
724 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500725#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000726 case 2: /* Time offset */
Wolfgang Denk135e19b2007-09-18 21:36:35 +0200727 NetCopyLong ((ulong *)&NetTimeOffset, (ulong *) (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000728 NetTimeOffset = ntohl (NetTimeOffset);
729 break;
730#endif
wdenk232c1502004-03-12 00:14:09 +0000731 case 3:
732 NetCopyIP (&NetOurGatewayIP, (popt + 2));
733 break;
734 case 6:
735 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500736#if defined(CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000737 if (*(popt + 1) > 4) {
738 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
739 }
stroesefe389a82003-08-28 14:17:32 +0000740#endif
wdenk232c1502004-03-12 00:14:09 +0000741 break;
742 case 12:
743 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
744 memcpy (&NetOurHostName, popt + 2, size);
745 NetOurHostName[size] = 0;
746 break;
747 case 15: /* Ignore Domain Name Option */
748 break;
749 case 17:
750 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
751 memcpy (&NetOurRootPath, popt + 2, size);
752 NetOurRootPath[size] = 0;
753 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500754#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000755 case 42: /* NTP server IP */
756 NetCopyIP (&NetNtpServerIP, (popt + 2));
757 break;
758#endif
wdenk232c1502004-03-12 00:14:09 +0000759 case 51:
760 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
761 break;
762 case 53: /* Ignore Message Type Option */
763 break;
764 case 54:
765 NetCopyIP (&NetDHCPServerIP, (popt + 2));
766 break;
767 case 58: /* Ignore Renewal Time Option */
768 break;
769 case 59: /* Ignore Rebinding Time Option */
770 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100771 case 66: /* Ignore TFTP server name */
772 break;
773 case 67: /* vendor opt bootfile */
774 /*
775 * I can't use dhcp_vendorex_proc here because I need
776 * to write into the bootp packet - even then I had to
777 * pass the bootp packet pointer into here as the
778 * second arg
779 */
780 size = truncate_sz ("Opt Boot File",
781 sizeof(bp->bp_file),
782 oplen);
783 if (bp->bp_file[0] == '\0' && size > 0) {
784 /*
785 * only use vendor boot file if we didn't
786 * receive a boot file in the main non-vendor
787 * part of the packet - god only knows why
788 * some vendors chose not to use this perfectly
789 * good spot to store the boot file (join on
790 * Tru64 Unix) it seems mind bogglingly crazy
791 * to me
792 */
793 printf("*** WARNING: using vendor "
794 "optional boot file\n");
795 memcpy(bp->bp_file, popt + 2, size);
796 bp->bp_file[size] = '\0';
797 }
798 break;
wdenk232c1502004-03-12 00:14:09 +0000799 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500800#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000801 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000802 break;
wdenk3861aa52002-09-27 23:19:37 +0000803#endif
wdenk232c1502004-03-12 00:14:09 +0000804 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
805 break;
wdenk3861aa52002-09-27 23:19:37 +0000806 }
807 popt += oplen + 2; /* Process next option */
808 }
809}
810
811static int DhcpMessageType(unsigned char *popt)
812{
813 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
814 return -1;
815
816 popt += 4;
817 while ( *popt != 0xff ) {
818 if ( *popt == 53 ) /* DHCP Message Type */
819 return *(popt + 2);
820 popt += *(popt + 1) + 2; /* Scan through all options */
821 }
822 return -1;
823}
824
wdenk3e386912003-04-05 00:53:31 +0000825static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000826{
827 volatile uchar *pkt, *iphdr;
828 Bootp_t *bp;
829 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000830 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000831
832 debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
833 pkt = NetTxPacket;
834 memset ((void*)pkt, 0, PKTSIZE);
835
wdenka3d991b2004-04-15 21:48:45 +0000836 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000837
838 iphdr = pkt; /* We'll need this later to set proper pkt size */
839 pkt += IP_HDR_SIZE;
840
841 bp = (Bootp_t *)pkt;
842 bp->bp_op = OP_BOOTREQUEST;
843 bp->bp_htype = HWT_ETHER;
844 bp->bp_hlen = HWL_ETHER;
845 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200846 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammiae5c794e2007-10-29 17:40:35 -0400847 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
848 * the server yet */
849
Wolfgang Denkc6686702006-10-12 00:01:08 +0200850 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200851 * RFC3046 requires Relay Agents to discard packets with
852 * nonzero and offered giaddr
853 */
854 NetWriteIP(&bp->bp_giaddr, 0);
855
wdenk3861aa52002-09-27 23:19:37 +0000856 memcpy (bp->bp_chaddr, NetOurEther, 6);
857
858 /*
859 * ID is the id of the OFFER packet
860 */
861
862 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
863
864 /*
865 * Copy options from OFFER packet if present
866 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400867
868 /* Copy offered IP into the parameters request list */
869 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200870 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000871
872 pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen;
873 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
874 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
875
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700876 debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100877#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
878 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
879#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000880 NetSendPacket(NetTxPacket, pktlen);
881}
882
883/*
884 * Handle DHCP received packets.
885 */
886static void
887DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
888{
889 Bootp_t *bp = (Bootp_t *)pkt;
890
891 debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
892 src, dest, len, dhcp_state);
893
wdenk232c1502004-03-12 00:14:09 +0000894 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000895 return;
896
897 debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
898 src, dest, len, dhcp_state);
899
900 switch (dhcp_state) {
901 case SELECTING:
902 /*
903 * Wait an appropriate time for any potential DHCPOFFER packets
904 * to arrive. Then select one, and generate DHCPREQUEST response.
905 * If filename is in format we recognize, assume it is a valid
906 * OFFER from a server we want.
907 */
908 debug ("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200909#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000910 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200911 CONFIG_SYS_BOOTFILE_PREFIX,
912 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
913#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000914
915 debug ("TRANSITIONING TO REQUESTING STATE\n");
916 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000917
wdenk3861aa52002-09-27 23:19:37 +0000918 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100919 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000920
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200921 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000922 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200923#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000924 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200925#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000926
927 return;
928 break;
929 case REQUESTING:
930 debug ("DHCP State: REQUESTING\n");
931
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200932 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000933 char *s;
934
935 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100936 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk232c1502004-03-12 00:14:09 +0000937 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000938 dhcp_state = BOUND;
Mike Frysingerb6446b62009-02-17 00:00:53 -0500939 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
wdenk3861aa52002-09-27 23:19:37 +0000940
941 /* Obey the 'autoload' setting */
wdenk132ba5f2004-02-27 08:20:54 +0000942 if ((s = getenv("autoload")) != NULL) {
943 if (*s == 'n') {
944 /*
945 * Just use BOOTP to configure system;
946 * Do not use TFTP to load the bootfile.
947 */
948 NetState = NETLOOP_SUCCESS;
949 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500950#if defined(CONFIG_CMD_NFS)
wdenk132ba5f2004-02-27 08:20:54 +0000951 } else if (strcmp(s, "NFS") == 0) {
952 /*
953 * Use NFS to load the bootfile.
954 */
955 NfsStart();
956 return;
wdenk0e6d7982004-03-14 00:07:33 +0000957#endif
wdenk132ba5f2004-02-27 08:20:54 +0000958 }
wdenk3861aa52002-09-27 23:19:37 +0000959 }
wdenk73a8b272003-06-05 19:27:42 +0000960 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000961 return;
962 }
963 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200964 case BOUND:
965 /* DHCP client bound to address */
966 break;
wdenk3861aa52002-09-27 23:19:37 +0000967 default:
wdenk4b9206e2004-03-23 22:14:11 +0000968 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000969 break;
970 }
971
972}
973
974void DhcpRequest(void)
975{
976 BootpRequest();
977}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100978#endif /* CONFIG_CMD_DHCP */
wdenk3861aa52002-09-27 23:19:37 +0000979
Wolfgang Denk992742a2007-11-03 23:09:27 +0100980#endif /* CONFIG_CMD_NET */