blob: 0799ae2b0fc4a4f9103d21e6bd73c9cc64df3a91 [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
Jon Loeliger643d1ab2007-07-09 17:45:14 -050023#if defined(CONFIG_CMD_NET)
wdenk3861aa52002-09-27 23:19:37 +000024
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020025#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000026#ifndef CONFIG_NET_RETRY_COUNT
wdenk3861aa52002-09-27 23:19:37 +000027# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
28#else
wdenk232c1502004-03-12 00:14:09 +000029# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000030#endif
31
32#define PORT_BOOTPS 67 /* BOOTP server UDP port */
33#define PORT_BOOTPC 68 /* BOOTP client UDP port */
34
35#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000036#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000037#endif
38
39ulong BootpID;
40int BootpTry;
41#ifdef CONFIG_BOOTP_RANDOM_DELAY
42ulong seed1, seed2;
43#endif
44
Jon Loeliger643d1ab2007-07-09 17:45:14 -050045#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000046dhcp_state_t dhcp_state = INIT;
wdenk682011f2003-06-03 23:54:09 +000047unsigned long dhcp_leasetime = 0;
stroese759a51b2003-04-10 13:26:44 +000048IPaddr_t NetDHCPServerIP = 0;
wdenk3861aa52002-09-27 23:19:37 +000049static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len);
50
51/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000052#if 0
53static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000054{
55 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000056 case 1: return "DHCPDISCOVER"; break;
57 case 2: return "DHCPOFFER"; break;
58 case 3: return "DHCPREQUEST"; break;
59 case 4: return "DHCPDECLINE"; break;
60 case 5: return "DHCPACK"; break;
61 case 6: return "DHCPNACK"; break;
62 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000063 default: return "UNKNOWN/INVALID MSG TYPE"; break;
64 }
65}
wdenk3e386912003-04-05 00:53:31 +000066#endif
wdenk3861aa52002-09-27 23:19:37 +000067
Jon Loeliger1fe80d72007-07-09 22:08:34 -050068#if defined(CONFIG_BOOTP_VENDOREX)
wdenk3861aa52002-09-27 23:19:37 +000069extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
70extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
71#endif
72
Jon Loeliger610f2e92007-07-10 11:05:02 -050073#endif
wdenk3861aa52002-09-27 23:19:37 +000074
75static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
76{
77 Bootp_t *bp = (Bootp_t *) pkt;
78 int retval = 0;
79
80 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
81 retval = -1;
82 else if (len < sizeof (Bootp_t) - OPT_SIZE)
83 retval = -2;
84 else if (bp->bp_op != OP_BOOTREQUEST &&
85 bp->bp_op != OP_BOOTREPLY &&
86 bp->bp_op != DHCP_OFFER &&
87 bp->bp_op != DHCP_ACK &&
88 bp->bp_op != DHCP_NAK ) {
89 retval = -3;
90 }
91 else if (bp->bp_htype != HWT_ETHER)
92 retval = -4;
93 else if (bp->bp_hlen != HWL_ETHER)
94 retval = -5;
95 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
96 retval = -6;
97 }
98
Robin Getz0ebf04c2009-07-23 03:01:03 -040099 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000100
101 return retval;
102}
103
104/*
105 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
106 */
wdenk3e386912003-04-05 00:53:31 +0000107static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000108{
wdenk3d3befa2004-03-14 15:06:13 +0000109 IPaddr_t tmp_ip;
110
wdenk3861aa52002-09-27 23:19:37 +0000111 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400112#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000113 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
114 if (tmp_ip != 0)
115 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400116 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400117#endif
wdenk3d3befa2004-03-14 15:06:13 +0000118 if (strlen(bp->bp_file) > 0)
119 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000120
Robin Getz0ebf04c2009-07-23 03:01:03 -0400121 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000122
123 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000124 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000125 * not contain a new value
126 */
127 if (*BootFile) {
128 setenv ("bootfile", BootFile);
129 }
130}
131
132static int truncate_sz (const char *name, int maxlen, int curlen)
133{
134 if (curlen >= maxlen) {
135 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
136 name, curlen, maxlen);
137 curlen = maxlen - 1;
138 }
139 return (curlen);
140}
141
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500142#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000143
wdenk232c1502004-03-12 00:14:09 +0000144static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000145{
wdenk232c1502004-03-12 00:14:09 +0000146 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000147
Robin Getz0ebf04c2009-07-23 03:01:03 -0400148 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
wdenk232c1502004-03-12 00:14:09 +0000149 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000150
wdenk232c1502004-03-12 00:14:09 +0000151 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000152
wdenk232c1502004-03-12 00:14:09 +0000153 switch (*ext) {
154 /* Fixed length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700155 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000156 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000157 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000158 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700159 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000160 break;
wdenk232c1502004-03-12 00:14:09 +0000161 /* Variable length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700162 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000163 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000164 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000165 }
166 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700167 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000168 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700169 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000170 break;
171 case 6:
172 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000173 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000174 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500175#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000176 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000177 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000178 }
179#endif
wdenk3861aa52002-09-27 23:19:37 +0000180 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700181 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000182 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700183 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000184 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700185 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000186 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700187 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000188 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700189 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000190 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700191 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000192 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000193 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
194 memcpy (&NetOurHostName, ext + 2, size);
195 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000196 }
197 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700198 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000199 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000200 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000201 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000202 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000203 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700204 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000205 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700206 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000207 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700208 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000209 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700210 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000211 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000212 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
213 memcpy (&NetOurRootPath, ext + 2, size);
214 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000215 }
216 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700217 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000218 /*
wdenk8bde7f72003-06-27 21:31:46 +0000219 * This can be used to send the information of the
220 * vendor area in another file that the client can
221 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000222 */
223 break;
wdenk232c1502004-03-12 00:14:09 +0000224 /* IP host layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700225 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000226 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000227 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
228 memcpy (&NetOurNISDomain, ext + 2, size);
229 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000230 }
231 break;
wdenk232c1502004-03-12 00:14:09 +0000232 /* Application layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700233 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000234 /*
wdenk8bde7f72003-06-27 21:31:46 +0000235 * Binary information to exchange specific
236 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000237 */
238 break;
wdenk232c1502004-03-12 00:14:09 +0000239 /* Reserved (custom) fields (128..254) */
240 }
wdenk3861aa52002-09-27 23:19:37 +0000241}
242
wdenk232c1502004-03-12 00:14:09 +0000243static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000244{
wdenk232c1502004-03-12 00:14:09 +0000245 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000246
Robin Getz0ebf04c2009-07-23 03:01:03 -0400247 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000248
wdenk232c1502004-03-12 00:14:09 +0000249 while ((ext < end) && (*ext != 0xff)) {
250 if (*ext == 0) {
251 ext++;
252 } else {
253 u8 *opt = ext;
254
255 ext += ext[1] + 2;
256 if (ext <= end)
257 BootpVendorFieldProcess (opt);
258 }
wdenk3861aa52002-09-27 23:19:37 +0000259 }
wdenk3861aa52002-09-27 23:19:37 +0000260
Robin Getz0ebf04c2009-07-23 03:01:03 -0400261 debug("[BOOTP] Received fields: \n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500262 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400263 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000264
Mike Frysingerb6446b62009-02-17 00:00:53 -0500265 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400266 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000267
Robin Getz0ebf04c2009-07-23 03:01:03 -0400268 if (NetBootFileSize)
269 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000270
Robin Getz0ebf04c2009-07-23 03:01:03 -0400271 if (NetOurHostName[0])
272 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000273
Robin Getz0ebf04c2009-07-23 03:01:03 -0400274 if (NetOurRootPath[0])
275 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000276
Robin Getz0ebf04c2009-07-23 03:01:03 -0400277 if (NetOurNISDomain[0])
278 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000279
Robin Getz0ebf04c2009-07-23 03:01:03 -0400280 if (NetBootFileSize)
281 debug("NetBootFileSize: %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000282}
wdenk3861aa52002-09-27 23:19:37 +0000283/*
284 * Handle a BOOTP received packet.
285 */
286static void
287BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
288{
289 Bootp_t *bp;
290 char *s;
291
Robin Getz0ebf04c2009-07-23 03:01:03 -0400292 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000293 src, dest, len, sizeof (Bootp_t));
294
295 bp = (Bootp_t *)pkt;
296
wdenk232c1502004-03-12 00:14:09 +0000297 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000298 return;
299
300 /*
wdenk232c1502004-03-12 00:14:09 +0000301 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000302 */
303#ifdef CONFIG_STATUS_LED
304 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
305#endif
306
307 BootpCopyNetParams(bp); /* Store net parameters from reply */
308
309 /* Retrieve extended information (we must parse the vendor area) */
310 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200311 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000312
313 NetSetTimeout(0, (thand_f *)0);
314
Robin Getz0ebf04c2009-07-23 03:01:03 -0400315 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000316
wdenk132ba5f2004-02-27 08:20:54 +0000317 if ((s = getenv("autoload")) != NULL) {
318 if (*s == 'n') {
319 /*
320 * Just use BOOTP to configure system;
321 * Do not use TFTP to load the bootfile.
322 */
323 NetState = NETLOOP_SUCCESS;
324 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500325#if defined(CONFIG_CMD_NFS)
wdenk132ba5f2004-02-27 08:20:54 +0000326 } else if (strcmp(s, "NFS") == 0) {
327 /*
328 * Use NFS to load the bootfile.
329 */
330 NfsStart();
331 return;
wdenk0e6d7982004-03-14 00:07:33 +0000332#endif
wdenk132ba5f2004-02-27 08:20:54 +0000333 }
wdenk3861aa52002-09-27 23:19:37 +0000334 }
335
wdenk73a8b272003-06-05 19:27:42 +0000336 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000337}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500338#endif
wdenk3861aa52002-09-27 23:19:37 +0000339
340/*
341 * Timeout on BOOTP/DHCP request.
342 */
343static void
344BootpTimeout(void)
345{
346 if (BootpTry >= TIMEOUT_COUNT) {
347 puts ("\nRetry count exceeded; starting again\n");
348 NetStartAgain ();
349 } else {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200350 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000351 BootpRequest ();
352 }
353}
354
355/*
356 * Initialize BOOTP extension fields in the request.
357 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500358#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000359static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000360{
wdenk232c1502004-03-12 00:14:09 +0000361 u8 *start = e;
362 u8 *cnt;
363
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500364#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000365 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000366#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500367#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200368 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000369#endif
wdenk3861aa52002-09-27 23:19:37 +0000370
wdenk232c1502004-03-12 00:14:09 +0000371 *e++ = 99; /* RFC1048 Magic Cookie */
372 *e++ = 130;
373 *e++ = 83;
374 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000375
wdenk232c1502004-03-12 00:14:09 +0000376 *e++ = 53; /* DHCP Message Type */
377 *e++ = 1;
378 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000379
wdenk232c1502004-03-12 00:14:09 +0000380 *e++ = 57; /* Maximum DHCP Message Size */
381 *e++ = 2;
382 *e++ = (576 - 312 + OPT_SIZE) >> 8;
383 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000384
wdenk232c1502004-03-12 00:14:09 +0000385 if (ServerID) {
386 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000387
wdenk232c1502004-03-12 00:14:09 +0000388 *e++ = 54; /* ServerID */
389 *e++ = 4;
390 *e++ = tmp >> 24;
391 *e++ = tmp >> 16;
392 *e++ = tmp >> 8;
393 *e++ = tmp & 0xff;
394 }
wdenk3861aa52002-09-27 23:19:37 +0000395
wdenk232c1502004-03-12 00:14:09 +0000396 if (RequestedIP) {
397 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000398
wdenk232c1502004-03-12 00:14:09 +0000399 *e++ = 50; /* Requested IP */
400 *e++ = 4;
401 *e++ = tmp >> 24;
402 *e++ = tmp >> 16;
403 *e++ = tmp >> 8;
404 *e++ = tmp & 0xff;
405 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500406#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000407 if ((hostname = getenv ("hostname"))) {
408 int hostnamelen = strlen (hostname);
409
410 *e++ = 12; /* Hostname */
411 *e++ = hostnamelen;
412 memcpy (e, hostname, hostnamelen);
413 e += hostnamelen;
414 }
stroesefe389a82003-08-28 14:17:32 +0000415#endif
416
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500417#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000418 if ((x = dhcp_vendorex_prep (e)))
419 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000420#endif
421
wdenk232c1502004-03-12 00:14:09 +0000422 *e++ = 55; /* Parameter Request List */
423 cnt = e++; /* Pointer to count of requested items */
424 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500425#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000426 *e++ = 1; /* Subnet Mask */
427 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000428#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500429#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000430 *e++ = 2;
431 *cnt += 1;
432#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500433#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000434 *e++ = 3; /* Router Option */
435 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000436#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500437#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000438 *e++ = 6; /* DNS Server(s) */
439 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000440#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500441#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000442 *e++ = 12; /* Hostname */
443 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000444#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500445#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000446 *e++ = 13; /* Boot File Size */
447 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000448#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500449#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000450 *e++ = 17; /* Boot path */
451 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000452#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500453#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000454 *e++ = 40; /* NIS Domain name request */
455 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000456#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500457#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000458 *e++ = 42;
459 *cnt += 1;
460#endif
wdenk232c1502004-03-12 00:14:09 +0000461 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000462
wdenk232c1502004-03-12 00:14:09 +0000463 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000464#ifdef CONFIG_DHCP_MIN_EXT_LEN
wdenk232c1502004-03-12 00:14:09 +0000465 while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN)
466 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000467#endif
468
wdenk232c1502004-03-12 00:14:09 +0000469 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000470}
471
Jon Loeliger610f2e92007-07-10 11:05:02 -0500472#else
wdenk3861aa52002-09-27 23:19:37 +0000473/*
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500474 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000475 */
wdenk232c1502004-03-12 00:14:09 +0000476static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000477{
wdenk232c1502004-03-12 00:14:09 +0000478 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000479
wdenk232c1502004-03-12 00:14:09 +0000480 *e++ = 99; /* RFC1048 Magic Cookie */
481 *e++ = 130;
482 *e++ = 83;
483 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000484
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500485#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000486 *e++ = 53; /* DHCP Message Type */
487 *e++ = 1;
488 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000489
wdenk232c1502004-03-12 00:14:09 +0000490 *e++ = 57; /* Maximum DHCP Message Size */
491 *e++ = 2;
492 *e++ = (576 - 312 + OPT_SIZE) >> 16;
493 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500494#endif
wdenk3861aa52002-09-27 23:19:37 +0000495
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500496#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000497 *e++ = 1; /* Subnet mask request */
498 *e++ = 4;
499 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000500#endif
501
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500502#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000503 *e++ = 3; /* Default gateway request */
504 *e++ = 4;
505 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000506#endif
507
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500508#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000509 *e++ = 6; /* Domain Name Server */
510 *e++ = 4;
511 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000512#endif
513
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500514#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000515 *e++ = 12; /* Host name request */
516 *e++ = 32;
517 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000518#endif
519
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500520#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000521 *e++ = 13; /* Boot file size */
522 *e++ = 2;
523 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000524#endif
525
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500526#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000527 *e++ = 17; /* Boot path */
528 *e++ = 32;
529 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000530#endif
531
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500532#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000533 *e++ = 40; /* NIS Domain name request */
534 *e++ = 32;
535 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
537
wdenk232c1502004-03-12 00:14:09 +0000538 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000539
wdenk232c1502004-03-12 00:14:09 +0000540 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000541}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500542#endif
wdenk3861aa52002-09-27 23:19:37 +0000543
544void
545BootpRequest (void)
546{
547 volatile uchar *pkt, *iphdr;
548 Bootp_t *bp;
549 int ext_len, pktlen, iplen;
550
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500551#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000552 dhcp_state = INIT;
553#endif
554
555#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
556 unsigned char bi_enetaddr[6];
557 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000558 ulong tst1, tst2, sum, m_mask, m_value = 0;
559
560 if (BootpTry ==0) {
561 /* get our mac */
Mike Frysinger95823ca2009-02-11 18:23:48 -0500562 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000563
Robin Getz0ebf04c2009-07-23 03:01:03 -0400564 debug("BootpRequest => Our Mac: ");
565 for (reg=0; reg<6; reg++)
566 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000567
568 /* Mac-Manipulation 2 get seed1 */
569 tst1=0;
570 tst2=0;
571 for (reg=2; reg<6; reg++) {
572 tst1 = tst1 << 8;
573 tst1 = tst1 | bi_enetaddr[reg];
574 }
575 for (reg=0; reg<2; reg++) {
576 tst2 = tst2 | bi_enetaddr[reg];
577 tst2 = tst2 << 8;
578 }
579
580 seed1 = tst1^tst2;
581
582 /* Mirror seed1*/
583 m_mask=0x1;
584 for (reg=1;reg<=32;reg++) {
585 m_value |= (m_mask & seed1);
586 seed1 = seed1 >> 1;
587 m_value = m_value << 1;
588 }
589 seed1 = m_value;
590 seed2 = 0xB78D0945;
591 }
592
593 /* Random Number Generator */
594
595 for (reg=0;reg<=0;reg++) {
596 sum = seed1 + seed2;
597 if (sum < seed1 || sum < seed2)
598 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000599 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000600 seed1 = sum;
601
602 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
603 sum = sum >> (22-BootpTry);
604 } else { /*After 3rd BOOTP request max 8192 * 1ms */
605 sum = sum >> 19;
606 }
607 }
608
609 printf ("Random delay: %ld ms...\n", sum);
610 for (reg=0; reg <sum; reg++) {
611 udelay(1000); /*Wait 1ms*/
612 }
613#endif /* CONFIG_BOOTP_RANDOM_DELAY */
614
615 printf("BOOTP broadcast %d\n", ++BootpTry);
616 pkt = NetTxPacket;
617 memset ((void*)pkt, 0, PKTSIZE);
618
wdenka3d991b2004-04-15 21:48:45 +0000619 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000620
621 /*
622 * Next line results in incorrect packet size being transmitted, resulting
623 * in errors in some DHCP servers, reporting missing bytes. Size must be
624 * set in packet header after extension length has been determined.
625 * C. Hallinan, DS4.COM, Inc.
626 */
627 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
628 iphdr = pkt; /* We need this later for NetSetIP() */
629 pkt += IP_HDR_SIZE;
630
631 bp = (Bootp_t *)pkt;
632 bp->bp_op = OP_BOOTREQUEST;
633 bp->bp_htype = HWT_ETHER;
634 bp->bp_hlen = HWL_ETHER;
635 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200636 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000637 NetWriteIP(&bp->bp_ciaddr, 0);
638 NetWriteIP(&bp->bp_yiaddr, 0);
639 NetWriteIP(&bp->bp_siaddr, 0);
640 NetWriteIP(&bp->bp_giaddr, 0);
641 memcpy (bp->bp_chaddr, NetOurEther, 6);
642 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
643
644 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500645#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200646 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000647#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200648 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500649#endif
wdenk3861aa52002-09-27 23:19:37 +0000650
651 /*
652 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200653 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000654 */
655 BootpID = ((ulong)NetOurEther[2] << 24)
656 | ((ulong)NetOurEther[3] << 16)
657 | ((ulong)NetOurEther[4] << 8)
658 | (ulong)NetOurEther[5];
659 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000660 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000661 NetCopyLong(&bp->bp_id, &BootpID);
662
663 /*
664 * Calculate proper packet lengths taking into account the
665 * variable size of the options field
666 */
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200667 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000668 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
669 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200670 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000671
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500672#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000673 dhcp_state = SELECTING;
674 NetSetHandler(DhcpHandler);
675#else
676 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500677#endif
wdenk3861aa52002-09-27 23:19:37 +0000678 NetSendPacket(NetTxPacket, pktlen);
679}
680
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500681#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100682static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000683{
wdenk3e386912003-04-05 00:53:31 +0000684 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000685 int oplen, size;
686
wdenk232c1502004-03-12 00:14:09 +0000687 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000688 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000689 switch (*popt) {
690 case 1:
691 NetCopyIP (&NetOurSubnetMask, (popt + 2));
692 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500693#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000694 case 2: /* Time offset */
Wolfgang Denk135e19b2007-09-18 21:36:35 +0200695 NetCopyLong ((ulong *)&NetTimeOffset, (ulong *) (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000696 NetTimeOffset = ntohl (NetTimeOffset);
697 break;
698#endif
wdenk232c1502004-03-12 00:14:09 +0000699 case 3:
700 NetCopyIP (&NetOurGatewayIP, (popt + 2));
701 break;
702 case 6:
703 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500704#if defined(CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000705 if (*(popt + 1) > 4) {
706 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
707 }
stroesefe389a82003-08-28 14:17:32 +0000708#endif
wdenk232c1502004-03-12 00:14:09 +0000709 break;
710 case 12:
711 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
712 memcpy (&NetOurHostName, popt + 2, size);
713 NetOurHostName[size] = 0;
714 break;
715 case 15: /* Ignore Domain Name Option */
716 break;
717 case 17:
718 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
719 memcpy (&NetOurRootPath, popt + 2, size);
720 NetOurRootPath[size] = 0;
721 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500722#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000723 case 42: /* NTP server IP */
724 NetCopyIP (&NetNtpServerIP, (popt + 2));
725 break;
726#endif
wdenk232c1502004-03-12 00:14:09 +0000727 case 51:
728 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
729 break;
730 case 53: /* Ignore Message Type Option */
731 break;
732 case 54:
733 NetCopyIP (&NetDHCPServerIP, (popt + 2));
734 break;
735 case 58: /* Ignore Renewal Time Option */
736 break;
737 case 59: /* Ignore Rebinding Time Option */
738 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100739 case 66: /* Ignore TFTP server name */
740 break;
741 case 67: /* vendor opt bootfile */
742 /*
743 * I can't use dhcp_vendorex_proc here because I need
744 * to write into the bootp packet - even then I had to
745 * pass the bootp packet pointer into here as the
746 * second arg
747 */
748 size = truncate_sz ("Opt Boot File",
749 sizeof(bp->bp_file),
750 oplen);
751 if (bp->bp_file[0] == '\0' && size > 0) {
752 /*
753 * only use vendor boot file if we didn't
754 * receive a boot file in the main non-vendor
755 * part of the packet - god only knows why
756 * some vendors chose not to use this perfectly
757 * good spot to store the boot file (join on
758 * Tru64 Unix) it seems mind bogglingly crazy
759 * to me
760 */
761 printf("*** WARNING: using vendor "
762 "optional boot file\n");
763 memcpy(bp->bp_file, popt + 2, size);
764 bp->bp_file[size] = '\0';
765 }
766 break;
wdenk232c1502004-03-12 00:14:09 +0000767 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500768#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000769 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000770 break;
wdenk3861aa52002-09-27 23:19:37 +0000771#endif
wdenk232c1502004-03-12 00:14:09 +0000772 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
773 break;
wdenk3861aa52002-09-27 23:19:37 +0000774 }
775 popt += oplen + 2; /* Process next option */
776 }
777}
778
779static int DhcpMessageType(unsigned char *popt)
780{
781 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
782 return -1;
783
784 popt += 4;
785 while ( *popt != 0xff ) {
786 if ( *popt == 53 ) /* DHCP Message Type */
787 return *(popt + 2);
788 popt += *(popt + 1) + 2; /* Scan through all options */
789 }
790 return -1;
791}
792
wdenk3e386912003-04-05 00:53:31 +0000793static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000794{
795 volatile uchar *pkt, *iphdr;
796 Bootp_t *bp;
797 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000798 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000799
Robin Getz0ebf04c2009-07-23 03:01:03 -0400800 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000801 pkt = NetTxPacket;
802 memset ((void*)pkt, 0, PKTSIZE);
803
wdenka3d991b2004-04-15 21:48:45 +0000804 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000805
806 iphdr = pkt; /* We'll need this later to set proper pkt size */
807 pkt += IP_HDR_SIZE;
808
809 bp = (Bootp_t *)pkt;
810 bp->bp_op = OP_BOOTREQUEST;
811 bp->bp_htype = HWT_ETHER;
812 bp->bp_hlen = HWL_ETHER;
813 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200814 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammiae5c794e2007-10-29 17:40:35 -0400815 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
816 * the server yet */
817
Wolfgang Denkc6686702006-10-12 00:01:08 +0200818 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200819 * RFC3046 requires Relay Agents to discard packets with
820 * nonzero and offered giaddr
821 */
822 NetWriteIP(&bp->bp_giaddr, 0);
823
wdenk3861aa52002-09-27 23:19:37 +0000824 memcpy (bp->bp_chaddr, NetOurEther, 6);
825
826 /*
827 * ID is the id of the OFFER packet
828 */
829
830 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
831
832 /*
833 * Copy options from OFFER packet if present
834 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400835
836 /* Copy offered IP into the parameters request list */
837 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200838 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000839
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200840 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000841 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
842 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
843
Robin Getz0ebf04c2009-07-23 03:01:03 -0400844 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100845#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
846 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
847#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000848 NetSendPacket(NetTxPacket, pktlen);
849}
850
851/*
852 * Handle DHCP received packets.
853 */
854static void
855DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
856{
857 Bootp_t *bp = (Bootp_t *)pkt;
858
Robin Getz0ebf04c2009-07-23 03:01:03 -0400859 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000860 src, dest, len, dhcp_state);
861
wdenk232c1502004-03-12 00:14:09 +0000862 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000863 return;
864
Robin Getz0ebf04c2009-07-23 03:01:03 -0400865 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000866 src, dest, len, dhcp_state);
867
868 switch (dhcp_state) {
869 case SELECTING:
870 /*
871 * Wait an appropriate time for any potential DHCPOFFER packets
872 * to arrive. Then select one, and generate DHCPREQUEST response.
873 * If filename is in format we recognize, assume it is a valid
874 * OFFER from a server we want.
875 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400876 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200877#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000878 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200879 CONFIG_SYS_BOOTFILE_PREFIX,
880 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
881#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000882
Robin Getz0ebf04c2009-07-23 03:01:03 -0400883 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000884 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000885
wdenk3861aa52002-09-27 23:19:37 +0000886 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100887 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000888
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200889 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000890 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200891#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000892 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200893#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000894
895 return;
896 break;
897 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400898 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000899
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200900 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000901 char *s;
902
903 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100904 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk232c1502004-03-12 00:14:09 +0000905 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000906 dhcp_state = BOUND;
Mike Frysingerb6446b62009-02-17 00:00:53 -0500907 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
wdenk3861aa52002-09-27 23:19:37 +0000908
909 /* Obey the 'autoload' setting */
wdenk132ba5f2004-02-27 08:20:54 +0000910 if ((s = getenv("autoload")) != NULL) {
911 if (*s == 'n') {
912 /*
913 * Just use BOOTP to configure system;
914 * Do not use TFTP to load the bootfile.
915 */
916 NetState = NETLOOP_SUCCESS;
917 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500918#if defined(CONFIG_CMD_NFS)
wdenk132ba5f2004-02-27 08:20:54 +0000919 } else if (strcmp(s, "NFS") == 0) {
920 /*
921 * Use NFS to load the bootfile.
922 */
923 NfsStart();
924 return;
wdenk0e6d7982004-03-14 00:07:33 +0000925#endif
wdenk132ba5f2004-02-27 08:20:54 +0000926 }
wdenk3861aa52002-09-27 23:19:37 +0000927 }
wdenk73a8b272003-06-05 19:27:42 +0000928 TftpStart();
wdenk3861aa52002-09-27 23:19:37 +0000929 return;
930 }
931 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200932 case BOUND:
933 /* DHCP client bound to address */
934 break;
wdenk3861aa52002-09-27 23:19:37 +0000935 default:
wdenk4b9206e2004-03-23 22:14:11 +0000936 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000937 break;
938 }
939
940}
941
942void DhcpRequest(void)
943{
944 BootpRequest();
945}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100946#endif /* CONFIG_CMD_DHCP */
wdenk3861aa52002-09-27 23:19:37 +0000947
Wolfgang Denk992742a2007-11-03 23:09:27 +0100948#endif /* CONFIG_CMD_NET */