blob: f5adce4f5e34d7f40fe67bf74e85da1cc35865dc [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
34#if (CONFIG_COMMANDS & CFG_CMD_NET)
35
36#define TIMEOUT 5 /* Seconds 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
56#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
57dhcp_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
79#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
80extern 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
84#endif /* CFG_CMD_DHCP */
85
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{
120 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
121 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
122 memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6);
123 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
124
125 debug ("Bootfile: %s\n", BootFile);
126
127 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000128 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000129 * not contain a new value
130 */
131 if (*BootFile) {
132 setenv ("bootfile", BootFile);
133 }
134}
135
136static int truncate_sz (const char *name, int maxlen, int curlen)
137{
138 if (curlen >= maxlen) {
139 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
140 name, curlen, maxlen);
141 curlen = maxlen - 1;
142 }
143 return (curlen);
144}
145
146#if !(CONFIG_COMMANDS & CFG_CMD_DHCP)
147
wdenk232c1502004-03-12 00:14:09 +0000148static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000149{
wdenk232c1502004-03-12 00:14:09 +0000150 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000151
wdenk232c1502004-03-12 00:14:09 +0000152 debug_ext ("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
153 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000154
wdenk232c1502004-03-12 00:14:09 +0000155 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000156
wdenk232c1502004-03-12 00:14:09 +0000157 switch (*ext) {
158 /* Fixed length fields */
159 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000160 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000161 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000162 break;
wdenk232c1502004-03-12 00:14:09 +0000163 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000164 break;
wdenk232c1502004-03-12 00:14:09 +0000165 /* Variable length fields */
166 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000167 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000168 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000169 }
170 break;
wdenk232c1502004-03-12 00:14:09 +0000171 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000172 break;
wdenk232c1502004-03-12 00:14:09 +0000173 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000174 break;
175 case 6:
176 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000177 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000178 }
stroesefe389a82003-08-28 14:17:32 +0000179#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
180 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000181 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000182 }
183#endif
wdenk3861aa52002-09-27 23:19:37 +0000184 break;
wdenk232c1502004-03-12 00:14:09 +0000185 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000186 break;
wdenk232c1502004-03-12 00:14:09 +0000187 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000188 break;
wdenk232c1502004-03-12 00:14:09 +0000189 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000190 break;
wdenk232c1502004-03-12 00:14:09 +0000191 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000192 break;
wdenk232c1502004-03-12 00:14:09 +0000193 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000194 break;
wdenk232c1502004-03-12 00:14:09 +0000195 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000196 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000197 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
198 memcpy (&NetOurHostName, ext + 2, size);
199 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000200 }
201 break;
wdenk232c1502004-03-12 00:14:09 +0000202 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000203 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000204 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000205 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000206 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000207 break;
wdenk232c1502004-03-12 00:14:09 +0000208 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000209 break;
wdenk232c1502004-03-12 00:14:09 +0000210 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000211 break;
wdenk232c1502004-03-12 00:14:09 +0000212 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000213 break;
wdenk232c1502004-03-12 00:14:09 +0000214 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000215 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000216 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
217 memcpy (&NetOurRootPath, ext + 2, size);
218 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000219 }
220 break;
wdenk232c1502004-03-12 00:14:09 +0000221 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000222 /*
wdenk8bde7f72003-06-27 21:31:46 +0000223 * This can be used to send the information of the
224 * vendor area in another file that the client can
225 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000226 */
227 break;
wdenk232c1502004-03-12 00:14:09 +0000228 /* IP host layer fields */
229 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000230 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000231 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
232 memcpy (&NetOurNISDomain, ext + 2, size);
233 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000234 }
235 break;
wdenk232c1502004-03-12 00:14:09 +0000236 /* Application layer fields */
237 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000238 /*
wdenk8bde7f72003-06-27 21:31:46 +0000239 * Binary information to exchange specific
240 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000241 */
242 break;
wdenk232c1502004-03-12 00:14:09 +0000243 /* Reserved (custom) fields (128..254) */
244 }
wdenk3861aa52002-09-27 23:19:37 +0000245}
246
wdenk232c1502004-03-12 00:14:09 +0000247static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000248{
wdenk232c1502004-03-12 00:14:09 +0000249 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000250
wdenk232c1502004-03-12 00:14:09 +0000251 debug_ext ("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000252
wdenk232c1502004-03-12 00:14:09 +0000253 while ((ext < end) && (*ext != 0xff)) {
254 if (*ext == 0) {
255 ext++;
256 } else {
257 u8 *opt = ext;
258
259 ext += ext[1] + 2;
260 if (ext <= end)
261 BootpVendorFieldProcess (opt);
262 }
wdenk3861aa52002-09-27 23:19:37 +0000263 }
wdenk3861aa52002-09-27 23:19:37 +0000264
265#ifdef DEBUG_BOOTP_EXT
wdenk232c1502004-03-12 00:14:09 +0000266 printf ("[BOOTP] Received fields: \n");
267 if (NetOurSubnetMask) {
268 puts ("NetOurSubnetMask : ");
269 print_IPaddr (NetOurSubnetMask);
270 putc ('\n');
271 }
wdenk3861aa52002-09-27 23:19:37 +0000272
wdenk232c1502004-03-12 00:14:09 +0000273 if (NetOurGatewayIP) {
274 puts ("NetOurGatewayIP : ");
275 print_IPaddr (NetOurGatewayIP);
276 putc ('\n');
277 }
wdenk3861aa52002-09-27 23:19:37 +0000278
wdenk232c1502004-03-12 00:14:09 +0000279 if (NetBootFileSize) {
280 printf ("NetBootFileSize : %d\n", NetBootFileSize);
281 }
wdenk3861aa52002-09-27 23:19:37 +0000282
wdenk232c1502004-03-12 00:14:09 +0000283 if (NetOurHostName[0]) {
284 printf ("NetOurHostName : %s\n", NetOurHostName);
285 }
wdenk3861aa52002-09-27 23:19:37 +0000286
wdenk232c1502004-03-12 00:14:09 +0000287 if (NetOurRootPath[0]) {
288 printf ("NetOurRootPath : %s\n", NetOurRootPath);
289 }
wdenk3861aa52002-09-27 23:19:37 +0000290
wdenk232c1502004-03-12 00:14:09 +0000291 if (NetOurNISDomain[0]) {
292 printf ("NetOurNISDomain : %s\n", NetOurNISDomain);
293 }
wdenk3861aa52002-09-27 23:19:37 +0000294
wdenk232c1502004-03-12 00:14:09 +0000295 if (NetBootFileSize) {
296 printf ("NetBootFileSize: %d\n", NetBootFileSize);
297 }
298#endif /* DEBUG_BOOTP_EXT */
wdenk3861aa52002-09-27 23:19:37 +0000299}
wdenk3861aa52002-09-27 23:19:37 +0000300/*
301 * Handle a BOOTP received packet.
302 */
303static void
304BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
305{
306 Bootp_t *bp;
307 char *s;
308
309 debug ("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%d)\n",
310 src, dest, len, sizeof (Bootp_t));
311
312 bp = (Bootp_t *)pkt;
313
wdenk232c1502004-03-12 00:14:09 +0000314 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000315 return;
316
317 /*
wdenk232c1502004-03-12 00:14:09 +0000318 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000319 */
320#ifdef CONFIG_STATUS_LED
321 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
322#endif
323
324 BootpCopyNetParams(bp); /* Store net parameters from reply */
325
326 /* Retrieve extended information (we must parse the vendor area) */
327 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
328 BootpVendorProcess(&bp->bp_vend[4], len);
329
330 NetSetTimeout(0, (thand_f *)0);
331
332 debug ("Got good BOOTP\n");
333
wdenk132ba5f2004-02-27 08:20:54 +0000334 if ((s = getenv("autoload")) != NULL) {
335 if (*s == 'n') {
336 /*
337 * Just use BOOTP to configure system;
338 * Do not use TFTP to load the bootfile.
339 */
340 NetState = NETLOOP_SUCCESS;
341 return;
342 } else if (strcmp(s, "NFS") == 0) {
343 /*
344 * Use NFS to load the bootfile.
345 */
346 NfsStart();
347 return;
348 }
wdenk3861aa52002-09-27 23:19:37 +0000349 }
350
wdenk73a8b272003-06-05 19:27:42 +0000351 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000352}
353#endif /* !CFG_CMD_DHCP */
354
355/*
356 * Timeout on BOOTP/DHCP request.
357 */
358static void
359BootpTimeout(void)
360{
361 if (BootpTry >= TIMEOUT_COUNT) {
362 puts ("\nRetry count exceeded; starting again\n");
363 NetStartAgain ();
364 } else {
365 NetSetTimeout (TIMEOUT * CFG_HZ, BootpTimeout);
366 BootpRequest ();
367 }
368}
369
370/*
371 * Initialize BOOTP extension fields in the request.
372 */
373#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000374static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000375{
wdenk232c1502004-03-12 00:14:09 +0000376 u8 *start = e;
377 u8 *cnt;
378
wdenk3861aa52002-09-27 23:19:37 +0000379#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000380 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000381#endif
stroesefe389a82003-08-28 14:17:32 +0000382#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000383 uchar *hostname;
stroesefe389a82003-08-28 14:17:32 +0000384#endif
wdenk3861aa52002-09-27 23:19:37 +0000385
wdenk232c1502004-03-12 00:14:09 +0000386 *e++ = 99; /* RFC1048 Magic Cookie */
387 *e++ = 130;
388 *e++ = 83;
389 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000390
wdenk232c1502004-03-12 00:14:09 +0000391 *e++ = 53; /* DHCP Message Type */
392 *e++ = 1;
393 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000394
wdenk232c1502004-03-12 00:14:09 +0000395 *e++ = 57; /* Maximum DHCP Message Size */
396 *e++ = 2;
397 *e++ = (576 - 312 + OPT_SIZE) >> 8;
398 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000399
wdenk232c1502004-03-12 00:14:09 +0000400 if (ServerID) {
401 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000402
wdenk232c1502004-03-12 00:14:09 +0000403 *e++ = 54; /* ServerID */
404 *e++ = 4;
405 *e++ = tmp >> 24;
406 *e++ = tmp >> 16;
407 *e++ = tmp >> 8;
408 *e++ = tmp & 0xff;
409 }
wdenk3861aa52002-09-27 23:19:37 +0000410
wdenk232c1502004-03-12 00:14:09 +0000411 if (RequestedIP) {
412 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000413
wdenk232c1502004-03-12 00:14:09 +0000414 *e++ = 50; /* Requested IP */
415 *e++ = 4;
416 *e++ = tmp >> 24;
417 *e++ = tmp >> 16;
418 *e++ = tmp >> 8;
419 *e++ = tmp & 0xff;
420 }
stroesefe389a82003-08-28 14:17:32 +0000421#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000422 if ((hostname = getenv ("hostname"))) {
423 int hostnamelen = strlen (hostname);
424
425 *e++ = 12; /* Hostname */
426 *e++ = hostnamelen;
427 memcpy (e, hostname, hostnamelen);
428 e += hostnamelen;
429 }
stroesefe389a82003-08-28 14:17:32 +0000430#endif
431
wdenk3861aa52002-09-27 23:19:37 +0000432#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000433 if ((x = dhcp_vendorex_prep (e)))
434 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000435#endif
436
wdenk232c1502004-03-12 00:14:09 +0000437 *e++ = 55; /* Parameter Request List */
438 cnt = e++; /* Pointer to count of requested items */
439 *cnt = 0;
wdenk3861aa52002-09-27 23:19:37 +0000440#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000441 *e++ = 1; /* Subnet Mask */
442 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000443#endif
444#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000445 *e++ = 3; /* Router Option */
446 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000447#endif
448#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000449 *e++ = 6; /* DNS Server(s) */
450 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000451#endif
452#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000453 *e++ = 12; /* Hostname */
454 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000455#endif
456#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000457 *e++ = 13; /* Boot File Size */
458 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000459#endif
460#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000461 *e++ = 17; /* Boot path */
462 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000463#endif
464#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000465 *e++ = 40; /* NIS Domain name request */
466 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000467#endif
wdenk232c1502004-03-12 00:14:09 +0000468 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000469
wdenk232c1502004-03-12 00:14:09 +0000470 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000471#ifdef CONFIG_DHCP_MIN_EXT_LEN
wdenk232c1502004-03-12 00:14:09 +0000472 while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN)
473 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
475
wdenk232c1502004-03-12 00:14:09 +0000476 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000477}
478
479#else /* CFG_CMD_DHCP */
480/*
481 * Warning: no field size check - change CONFIG_BOOTP_MASK at your own risk!
482 */
wdenk232c1502004-03-12 00:14:09 +0000483static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000484{
wdenk232c1502004-03-12 00:14:09 +0000485 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000486
wdenk232c1502004-03-12 00:14:09 +0000487 *e++ = 99; /* RFC1048 Magic Cookie */
488 *e++ = 130;
489 *e++ = 83;
490 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000491
492#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000493 *e++ = 53; /* DHCP Message Type */
494 *e++ = 1;
495 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000496
wdenk232c1502004-03-12 00:14:09 +0000497 *e++ = 57; /* Maximum DHCP Message Size */
498 *e++ = 2;
499 *e++ = (576 - 312 + OPT_SIZE) >> 16;
500 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
501#endif /* CFG_CMD_DHCP */
wdenk3861aa52002-09-27 23:19:37 +0000502
503#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000504 *e++ = 1; /* Subnet mask request */
505 *e++ = 4;
506 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000507#endif
508
509#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000510 *e++ = 3; /* Default gateway request */
511 *e++ = 4;
512 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000513#endif
514
515#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000516 *e++ = 6; /* Domain Name Server */
517 *e++ = 4;
518 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000519#endif
520
521#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000522 *e++ = 12; /* Host name request */
523 *e++ = 32;
524 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000525#endif
526
527#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000528 *e++ = 13; /* Boot file size */
529 *e++ = 2;
530 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000531#endif
532
533#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000534 *e++ = 17; /* Boot path */
535 *e++ = 32;
536 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000537#endif
538
539#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000540 *e++ = 40; /* NIS Domain name request */
541 *e++ = 32;
542 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000543#endif
544
wdenk232c1502004-03-12 00:14:09 +0000545 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000546
wdenk232c1502004-03-12 00:14:09 +0000547 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000548}
549#endif /* CFG_CMD_DHCP */
550
551void
552BootpRequest (void)
553{
554 volatile uchar *pkt, *iphdr;
555 Bootp_t *bp;
556 int ext_len, pktlen, iplen;
557
558#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
559 dhcp_state = INIT;
560#endif
561
562#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
563 unsigned char bi_enetaddr[6];
564 int reg;
565 char *e,*s;
566 uchar tmp[64];
567 ulong tst1, tst2, sum, m_mask, m_value = 0;
568
569 if (BootpTry ==0) {
570 /* get our mac */
571 reg = getenv_r ("ethaddr", tmp, sizeof(tmp));
572 s = (reg > 0) ? tmp : NULL;
573
574 for (reg=0; reg<6; ++reg) {
575 bi_enetaddr[reg] = s ? simple_strtoul(s, &e, 16) : 0;
576 if (s) {
577 s = (*e) ? e+1 : e;
578 }
579 }
580#ifdef DEBUG
581 printf("BootpRequest => Our Mac: ");
582 for (reg=0; reg<6; reg++) {
583 printf ("%x%c",
584 bi_enetaddr[reg],
585 reg==5 ? '\n' : ':');
586 }
587#endif /* DEBUG */
588
589 /* Mac-Manipulation 2 get seed1 */
590 tst1=0;
591 tst2=0;
592 for (reg=2; reg<6; reg++) {
593 tst1 = tst1 << 8;
594 tst1 = tst1 | bi_enetaddr[reg];
595 }
596 for (reg=0; reg<2; reg++) {
597 tst2 = tst2 | bi_enetaddr[reg];
598 tst2 = tst2 << 8;
599 }
600
601 seed1 = tst1^tst2;
602
603 /* Mirror seed1*/
604 m_mask=0x1;
605 for (reg=1;reg<=32;reg++) {
606 m_value |= (m_mask & seed1);
607 seed1 = seed1 >> 1;
608 m_value = m_value << 1;
609 }
610 seed1 = m_value;
611 seed2 = 0xB78D0945;
612 }
613
614 /* Random Number Generator */
615
616 for (reg=0;reg<=0;reg++) {
617 sum = seed1 + seed2;
618 if (sum < seed1 || sum < seed2)
619 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000620 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000621 seed1 = sum;
622
623 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
624 sum = sum >> (22-BootpTry);
625 } else { /*After 3rd BOOTP request max 8192 * 1ms */
626 sum = sum >> 19;
627 }
628 }
629
630 printf ("Random delay: %ld ms...\n", sum);
631 for (reg=0; reg <sum; reg++) {
632 udelay(1000); /*Wait 1ms*/
633 }
634#endif /* CONFIG_BOOTP_RANDOM_DELAY */
635
636 printf("BOOTP broadcast %d\n", ++BootpTry);
637 pkt = NetTxPacket;
638 memset ((void*)pkt, 0, PKTSIZE);
639
640 NetSetEther(pkt, NetBcastAddr, PROT_IP);
641 pkt += ETHER_HDR_SIZE;
642
643 /*
644 * Next line results in incorrect packet size being transmitted, resulting
645 * in errors in some DHCP servers, reporting missing bytes. Size must be
646 * set in packet header after extension length has been determined.
647 * C. Hallinan, DS4.COM, Inc.
648 */
649 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
650 iphdr = pkt; /* We need this later for NetSetIP() */
651 pkt += IP_HDR_SIZE;
652
653 bp = (Bootp_t *)pkt;
654 bp->bp_op = OP_BOOTREQUEST;
655 bp->bp_htype = HWT_ETHER;
656 bp->bp_hlen = HWL_ETHER;
657 bp->bp_hops = 0;
658 bp->bp_secs = htons(get_timer(0) / CFG_HZ);
659 NetWriteIP(&bp->bp_ciaddr, 0);
660 NetWriteIP(&bp->bp_yiaddr, 0);
661 NetWriteIP(&bp->bp_siaddr, 0);
662 NetWriteIP(&bp->bp_giaddr, 0);
663 memcpy (bp->bp_chaddr, NetOurEther, 6);
664 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
665
666 /* Request additional information from the BOOTP/DHCP server */
667#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
668 ext_len = DhcpExtended(bp->bp_vend, DHCP_DISCOVER, 0, 0);
669#else
670 ext_len = BootpExtended(bp->bp_vend);
671#endif /* CFG_CMD_DHCP */
672
673 /*
674 * Bootp ID is the lower 4 bytes of our ethernet address
675 * plus the current time in HZ.
676 */
677 BootpID = ((ulong)NetOurEther[2] << 24)
678 | ((ulong)NetOurEther[3] << 16)
679 | ((ulong)NetOurEther[4] << 8)
680 | (ulong)NetOurEther[5];
681 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000682 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000683 NetCopyLong(&bp->bp_id, &BootpID);
684
685 /*
686 * Calculate proper packet lengths taking into account the
687 * variable size of the options field
688 */
689 pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + ext_len;
690 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
691 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
692 NetSetTimeout(SELECT_TIMEOUT * CFG_HZ, BootpTimeout);
693
694#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
695 dhcp_state = SELECTING;
696 NetSetHandler(DhcpHandler);
697#else
698 NetSetHandler(BootpHandler);
699#endif /* CFG_CMD_DHCP */
700 NetSendPacket(NetTxPacket, pktlen);
701}
702
703#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000704static void DhcpOptionsProcess (uchar * popt)
wdenk3861aa52002-09-27 23:19:37 +0000705{
wdenk3e386912003-04-05 00:53:31 +0000706 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000707 int oplen, size;
708
wdenk232c1502004-03-12 00:14:09 +0000709 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000710 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000711 switch (*popt) {
712 case 1:
713 NetCopyIP (&NetOurSubnetMask, (popt + 2));
714 break;
715 case 3:
716 NetCopyIP (&NetOurGatewayIP, (popt + 2));
717 break;
718 case 6:
719 NetCopyIP (&NetOurDNSIP, (popt + 2));
stroesefe389a82003-08-28 14:17:32 +0000720#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000721 if (*(popt + 1) > 4) {
722 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
723 }
stroesefe389a82003-08-28 14:17:32 +0000724#endif
wdenk232c1502004-03-12 00:14:09 +0000725 break;
726 case 12:
727 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
728 memcpy (&NetOurHostName, popt + 2, size);
729 NetOurHostName[size] = 0;
730 break;
731 case 15: /* Ignore Domain Name Option */
732 break;
733 case 17:
734 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
735 memcpy (&NetOurRootPath, popt + 2, size);
736 NetOurRootPath[size] = 0;
737 break;
738 case 51:
739 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
740 break;
741 case 53: /* Ignore Message Type Option */
742 break;
743 case 54:
744 NetCopyIP (&NetDHCPServerIP, (popt + 2));
745 break;
746 case 58: /* Ignore Renewal Time Option */
747 break;
748 case 59: /* Ignore Rebinding Time Option */
749 break;
750 default:
wdenk3861aa52002-09-27 23:19:37 +0000751#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000752 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000753 break;
wdenk3861aa52002-09-27 23:19:37 +0000754#endif
wdenk232c1502004-03-12 00:14:09 +0000755 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
756 break;
wdenk3861aa52002-09-27 23:19:37 +0000757 }
758 popt += oplen + 2; /* Process next option */
759 }
760}
761
762static int DhcpMessageType(unsigned char *popt)
763{
764 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
765 return -1;
766
767 popt += 4;
768 while ( *popt != 0xff ) {
769 if ( *popt == 53 ) /* DHCP Message Type */
770 return *(popt + 2);
771 popt += *(popt + 1) + 2; /* Scan through all options */
772 }
773 return -1;
774}
775
wdenk3e386912003-04-05 00:53:31 +0000776static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000777{
778 volatile uchar *pkt, *iphdr;
779 Bootp_t *bp;
780 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000781 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000782
783 debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
784 pkt = NetTxPacket;
785 memset ((void*)pkt, 0, PKTSIZE);
786
787 NetSetEther(pkt, NetBcastAddr, PROT_IP);
788 pkt += ETHER_HDR_SIZE;
789
790 iphdr = pkt; /* We'll need this later to set proper pkt size */
791 pkt += IP_HDR_SIZE;
792
793 bp = (Bootp_t *)pkt;
794 bp->bp_op = OP_BOOTREQUEST;
795 bp->bp_htype = HWT_ETHER;
796 bp->bp_hlen = HWL_ETHER;
797 bp->bp_hops = 0;
798 bp->bp_secs = htons(get_timer(0) / CFG_HZ);
799 NetCopyIP(&bp->bp_ciaddr, &bp_offer->bp_ciaddr); /* both in network byte order */
800 NetCopyIP(&bp->bp_yiaddr, &bp_offer->bp_yiaddr);
801 NetCopyIP(&bp->bp_siaddr, &bp_offer->bp_siaddr);
802 NetCopyIP(&bp->bp_giaddr, &bp_offer->bp_giaddr);
803 memcpy (bp->bp_chaddr, NetOurEther, 6);
804
805 /*
806 * ID is the id of the OFFER packet
807 */
808
809 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
810
811 /*
812 * Copy options from OFFER packet if present
813 */
wdenk47cd00f2003-03-06 13:39:27 +0000814 NetCopyIP(&OfferedIP, &bp->bp_yiaddr);
stroese759a51b2003-04-10 13:26:44 +0000815 extlen = DhcpExtended(bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000816
817 pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen;
818 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
819 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
820
821 debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
822 NetSendPacket(NetTxPacket, pktlen);
823}
824
825/*
826 * Handle DHCP received packets.
827 */
828static void
829DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
830{
831 Bootp_t *bp = (Bootp_t *)pkt;
832
833 debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
834 src, dest, len, dhcp_state);
835
wdenk232c1502004-03-12 00:14:09 +0000836 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000837 return;
838
839 debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
840 src, dest, len, dhcp_state);
841
842 switch (dhcp_state) {
843 case SELECTING:
844 /*
845 * Wait an appropriate time for any potential DHCPOFFER packets
846 * to arrive. Then select one, and generate DHCPREQUEST response.
847 * If filename is in format we recognize, assume it is a valid
848 * OFFER from a server we want.
849 */
850 debug ("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
851#ifdef CFG_BOOTFILE_PREFIX
852 if (strncmp(bp->bp_file,
853 CFG_BOOTFILE_PREFIX,
854 strlen(CFG_BOOTFILE_PREFIX)) == 0 ) {
855#endif /* CFG_BOOTFILE_PREFIX */
856
857 debug ("TRANSITIONING TO REQUESTING STATE\n");
858 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000859
wdenk3861aa52002-09-27 23:19:37 +0000860 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
861 DhcpOptionsProcess(&bp->bp_vend[4]);
862
wdenk232c1502004-03-12 00:14:09 +0000863 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000864
865 NetSetTimeout(TIMEOUT * CFG_HZ, BootpTimeout);
866 DhcpSendRequestPkt(bp);
867#ifdef CFG_BOOTFILE_PREFIX
868 }
869#endif /* CFG_BOOTFILE_PREFIX */
870
871 return;
872 break;
873 case REQUESTING:
874 debug ("DHCP State: REQUESTING\n");
875
876 if ( DhcpMessageType(bp->bp_vend) == DHCP_ACK ) {
877 char *s;
878
879 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
880 DhcpOptionsProcess(&bp->bp_vend[4]);
wdenk232c1502004-03-12 00:14:09 +0000881 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000882 dhcp_state = BOUND;
883 printf("DHCP client bound to address ");
884 print_IPaddr(NetOurIP);
885 printf("\n");
886
887 /* Obey the 'autoload' setting */
wdenk132ba5f2004-02-27 08:20:54 +0000888 if ((s = getenv("autoload")) != NULL) {
889 if (*s == 'n') {
890 /*
891 * Just use BOOTP to configure system;
892 * Do not use TFTP to load the bootfile.
893 */
894 NetState = NETLOOP_SUCCESS;
895 return;
896 } else if (strcmp(s, "NFS") == 0) {
897 /*
898 * Use NFS to load the bootfile.
899 */
900 NfsStart();
901 return;
902 }
wdenk3861aa52002-09-27 23:19:37 +0000903 }
wdenk73a8b272003-06-05 19:27:42 +0000904 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000905 return;
906 }
907 break;
908 default:
909 printf("DHCP: INVALID STATE\n");
910 break;
911 }
912
913}
914
915void DhcpRequest(void)
916{
917 BootpRequest();
918}
919#endif /* CFG_CMD_DHCP */
920
921#endif /* CFG_CMD_NET */