blob: d99eb571d71c5a46d408ce62d37a48530c7be6b4 [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
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +000020#include <linux/compiler.h>
wdenk3861aa52002-09-27 23:19:37 +000021
wdenk232c1502004-03-12 00:14:09 +000022#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000023
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020024#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000025#ifndef CONFIG_NET_RETRY_COUNT
wdenk3861aa52002-09-27 23:19:37 +000026# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
27#else
wdenk232c1502004-03-12 00:14:09 +000028# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000029#endif
30
31#define PORT_BOOTPS 67 /* BOOTP server UDP port */
32#define PORT_BOOTPC 68 /* BOOTP client UDP port */
33
34#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000035#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000036#endif
37
38ulong BootpID;
39int BootpTry;
40#ifdef CONFIG_BOOTP_RANDOM_DELAY
41ulong seed1, seed2;
42#endif
43
Jon Loeliger643d1ab2007-07-09 17:45:14 -050044#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000045dhcp_state_t dhcp_state = INIT;
wdenk682011f2003-06-03 23:54:09 +000046unsigned long dhcp_leasetime = 0;
stroese759a51b2003-04-10 13:26:44 +000047IPaddr_t NetDHCPServerIP = 0;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000048static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
49 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000050
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 */
John Rigby2c872ca2012-08-09 14:17:11 -060071u8 *__dhcp_vendorex_prep(u8 *e)
72{
73 char *ptr;
wdenk3861aa52002-09-27 23:19:37 +000074
John Rigby2c872ca2012-08-09 14:17:11 -060075 /* DHCP vendor-class-identifier = 60 */
76 if ((ptr = getenv("dhcp_vendor-class-identifier"))) {
77 *e++ = 60;
78 *e++ = strlen(ptr);
79 while (*ptr)
80 *e++ = *ptr++;
81 }
82 /* DHCP_CLIENT_IDENTIFIER = 61 */
83 if ((ptr = getenv("dhcp_client_id"))) {
84 *e++ = 61;
85 *e++ = strlen(ptr);
86 while (*ptr)
87 *e++ = *ptr++;
88 }
John Rigby2c872ca2012-08-09 14:17:11 -060089 return e;
90}
91
92u8 *__dhcp_vendorex_proc(u8 *popt)
93{
94 return NULL;
95}
96u8 *dhcp_vendorex_prep(u8 *e) __attribute__((weak, alias("__dhcp_vendorex_prep")));
97u8 *dhcp_vendorex_proc(u8 *e) __attribute__((weak, alias("__dhcp_vendorex_proc")));
Justin L Wernerdc80e002012-08-09 14:20:30 -060098
99#if defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED)
100# include "../net/magic.h"
101#endif
John Rigby2c872ca2012-08-09 14:17:11 -0600102#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -0500103#endif
wdenk3861aa52002-09-27 23:19:37 +0000104
105static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
106{
107 Bootp_t *bp = (Bootp_t *) pkt;
108 int retval = 0;
109
110 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
111 retval = -1;
112 else if (len < sizeof (Bootp_t) - OPT_SIZE)
113 retval = -2;
114 else if (bp->bp_op != OP_BOOTREQUEST &&
115 bp->bp_op != OP_BOOTREPLY &&
116 bp->bp_op != DHCP_OFFER &&
117 bp->bp_op != DHCP_ACK &&
118 bp->bp_op != DHCP_NAK ) {
119 retval = -3;
120 }
121 else if (bp->bp_htype != HWT_ETHER)
122 retval = -4;
123 else if (bp->bp_hlen != HWL_ETHER)
124 retval = -5;
125 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
126 retval = -6;
127 }
128
Robin Getz0ebf04c2009-07-23 03:01:03 -0400129 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000130
131 return retval;
132}
133
134/*
135 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
136 */
wdenk3e386912003-04-05 00:53:31 +0000137static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000138{
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +0000139 __maybe_unused IPaddr_t tmp_ip;
wdenk3d3befa2004-03-14 15:06:13 +0000140
wdenk3861aa52002-09-27 23:19:37 +0000141 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400142#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000143 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
144 if (tmp_ip != 0)
145 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400146 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400147#endif
wdenk3d3befa2004-03-14 15:06:13 +0000148 if (strlen(bp->bp_file) > 0)
149 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000150
Robin Getz0ebf04c2009-07-23 03:01:03 -0400151 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000152
153 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000154 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000155 * not contain a new value
156 */
157 if (*BootFile) {
158 setenv ("bootfile", BootFile);
159 }
160}
161
162static int truncate_sz (const char *name, int maxlen, int curlen)
163{
164 if (curlen >= maxlen) {
165 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
166 name, curlen, maxlen);
167 curlen = maxlen - 1;
168 }
169 return (curlen);
170}
171
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500172#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000173
wdenk232c1502004-03-12 00:14:09 +0000174static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000175{
wdenk232c1502004-03-12 00:14:09 +0000176 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000177
Robin Getz0ebf04c2009-07-23 03:01:03 -0400178 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
wdenk232c1502004-03-12 00:14:09 +0000179 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000180
wdenk232c1502004-03-12 00:14:09 +0000181 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000182
wdenk232c1502004-03-12 00:14:09 +0000183 switch (*ext) {
184 /* Fixed length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700185 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000186 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000187 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000188 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700189 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000190 break;
wdenk232c1502004-03-12 00:14:09 +0000191 /* Variable length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700192 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000193 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000194 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000195 }
196 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700197 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000198 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700199 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000200 break;
201 case 6:
202 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000203 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000204 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500205#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000206 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000207 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000208 }
209#endif
wdenk3861aa52002-09-27 23:19:37 +0000210 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700211 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000212 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700213 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000214 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700215 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000216 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700217 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000218 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700219 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000220 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700221 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000222 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000223 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
224 memcpy (&NetOurHostName, ext + 2, size);
225 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000226 }
227 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700228 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000229 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000230 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000231 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000232 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000233 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700234 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000235 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700236 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000237 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700238 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000239 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700240 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000241 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000242 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
243 memcpy (&NetOurRootPath, ext + 2, size);
244 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000245 }
246 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700247 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000248 /*
wdenk8bde7f72003-06-27 21:31:46 +0000249 * This can be used to send the information of the
250 * vendor area in another file that the client can
251 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000252 */
253 break;
wdenk232c1502004-03-12 00:14:09 +0000254 /* IP host layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700255 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000256 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000257 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
258 memcpy (&NetOurNISDomain, ext + 2, size);
259 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000260 }
261 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000262#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
263 case 42: /* NTP server IP */
264 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
265 break;
266#endif
wdenk232c1502004-03-12 00:14:09 +0000267 /* Application layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700268 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000269 /*
wdenk8bde7f72003-06-27 21:31:46 +0000270 * Binary information to exchange specific
271 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000272 */
273 break;
wdenk232c1502004-03-12 00:14:09 +0000274 /* Reserved (custom) fields (128..254) */
275 }
wdenk3861aa52002-09-27 23:19:37 +0000276}
277
wdenk232c1502004-03-12 00:14:09 +0000278static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000279{
wdenk232c1502004-03-12 00:14:09 +0000280 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000281
Robin Getz0ebf04c2009-07-23 03:01:03 -0400282 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000283
wdenk232c1502004-03-12 00:14:09 +0000284 while ((ext < end) && (*ext != 0xff)) {
285 if (*ext == 0) {
286 ext++;
287 } else {
288 u8 *opt = ext;
289
290 ext += ext[1] + 2;
291 if (ext <= end)
292 BootpVendorFieldProcess (opt);
293 }
wdenk3861aa52002-09-27 23:19:37 +0000294 }
wdenk3861aa52002-09-27 23:19:37 +0000295
Robin Getz0ebf04c2009-07-23 03:01:03 -0400296 debug("[BOOTP] Received fields: \n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500297 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400298 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000299
Mike Frysingerb6446b62009-02-17 00:00:53 -0500300 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400301 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000302
Robin Getz0ebf04c2009-07-23 03:01:03 -0400303 if (NetBootFileSize)
304 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000305
Robin Getz0ebf04c2009-07-23 03:01:03 -0400306 if (NetOurHostName[0])
307 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000308
Robin Getz0ebf04c2009-07-23 03:01:03 -0400309 if (NetOurRootPath[0])
310 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000311
Robin Getz0ebf04c2009-07-23 03:01:03 -0400312 if (NetOurNISDomain[0])
313 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000314
Robin Getz0ebf04c2009-07-23 03:01:03 -0400315 if (NetBootFileSize)
316 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000317
318#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
319 if (NetNtpServerIP)
320 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
321#endif
wdenk3861aa52002-09-27 23:19:37 +0000322}
Simon Glass09349862011-06-13 16:13:12 -0700323
wdenk3861aa52002-09-27 23:19:37 +0000324/*
325 * Handle a BOOTP received packet.
326 */
327static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000328BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
329 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000330{
331 Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000332
Robin Getz0ebf04c2009-07-23 03:01:03 -0400333 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000334 src, dest, len, sizeof (Bootp_t));
335
336 bp = (Bootp_t *)pkt;
337
wdenk232c1502004-03-12 00:14:09 +0000338 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000339 return;
340
341 /*
wdenk232c1502004-03-12 00:14:09 +0000342 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000343 */
344#ifdef CONFIG_STATUS_LED
345 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
346#endif
347
348 BootpCopyNetParams(bp); /* Store net parameters from reply */
349
350 /* Retrieve extended information (we must parse the vendor area) */
351 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200352 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000353
354 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000355 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000356
Robin Getz0ebf04c2009-07-23 03:01:03 -0400357 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000358
Simon Glasse4a3d572011-10-27 06:24:32 +0000359 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000360}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500361#endif
wdenk3861aa52002-09-27 23:19:37 +0000362
363/*
364 * Timeout on BOOTP/DHCP request.
365 */
366static void
367BootpTimeout(void)
368{
369 if (BootpTry >= TIMEOUT_COUNT) {
370 puts ("\nRetry count exceeded; starting again\n");
371 NetStartAgain ();
372 } else {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200373 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000374 BootpRequest ();
375 }
376}
377
378/*
379 * Initialize BOOTP extension fields in the request.
380 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500381#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000382static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000383{
wdenk232c1502004-03-12 00:14:09 +0000384 u8 *start = e;
385 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000386#if defined(CONFIG_BOOTP_PXE)
387 char *uuid;
388 size_t vci_strlen;
389 u16 clientarch;
390#endif
wdenk232c1502004-03-12 00:14:09 +0000391
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500392#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000393 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000394#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500395#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200396 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000397#endif
wdenk3861aa52002-09-27 23:19:37 +0000398
wdenk232c1502004-03-12 00:14:09 +0000399 *e++ = 99; /* RFC1048 Magic Cookie */
400 *e++ = 130;
401 *e++ = 83;
402 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000403
wdenk232c1502004-03-12 00:14:09 +0000404 *e++ = 53; /* DHCP Message Type */
405 *e++ = 1;
406 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000407
wdenk232c1502004-03-12 00:14:09 +0000408 *e++ = 57; /* Maximum DHCP Message Size */
409 *e++ = 2;
410 *e++ = (576 - 312 + OPT_SIZE) >> 8;
411 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000412
wdenk232c1502004-03-12 00:14:09 +0000413 if (ServerID) {
414 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000415
wdenk232c1502004-03-12 00:14:09 +0000416 *e++ = 54; /* ServerID */
417 *e++ = 4;
418 *e++ = tmp >> 24;
419 *e++ = tmp >> 16;
420 *e++ = tmp >> 8;
421 *e++ = tmp & 0xff;
422 }
wdenk3861aa52002-09-27 23:19:37 +0000423
wdenk232c1502004-03-12 00:14:09 +0000424 if (RequestedIP) {
425 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000426
wdenk232c1502004-03-12 00:14:09 +0000427 *e++ = 50; /* Requested IP */
428 *e++ = 4;
429 *e++ = tmp >> 24;
430 *e++ = tmp >> 16;
431 *e++ = tmp >> 8;
432 *e++ = tmp & 0xff;
433 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500434#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000435 if ((hostname = getenv ("hostname"))) {
436 int hostnamelen = strlen (hostname);
437
438 *e++ = 12; /* Hostname */
439 *e++ = hostnamelen;
440 memcpy (e, hostname, hostnamelen);
441 e += hostnamelen;
442 }
stroesefe389a82003-08-28 14:17:32 +0000443#endif
444
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000445#if defined(CONFIG_BOOTP_PXE)
446 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
447 *e++ = 93; /* Client System Architecture */
448 *e++ = 2;
449 *e++ = (clientarch >> 8) & 0xff;
450 *e++ = clientarch & 0xff;
451
452 *e++ = 94; /* Client Network Interface Identifier */
453 *e++ = 3;
454 *e++ = 1; /* type field for UNDI */
455 *e++ = 0; /* major revision */
456 *e++ = 0; /* minor revision */
457
458 uuid = getenv("pxeuuid");
459
460 if (uuid) {
461 if (uuid_str_valid(uuid)) {
462 *e++ = 97; /* Client Machine Identifier */
463 *e++ = 17;
464 *e++ = 0; /* type 0 - UUID */
465
466 uuid_str_to_bin(uuid, e);
467 e += 16;
468 } else {
469 printf("Invalid pxeuuid: %s\n", uuid);
470 }
471 }
472
473 *e++ = 60; /* Vendor Class Identifier */
474 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
475 *e++ = vci_strlen;
476 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
477 e += vci_strlen;
478#endif
479
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500480#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000481 if ((x = dhcp_vendorex_prep (e)))
482 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000483#endif
484
wdenk232c1502004-03-12 00:14:09 +0000485 *e++ = 55; /* Parameter Request List */
486 cnt = e++; /* Pointer to count of requested items */
487 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500488#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000489 *e++ = 1; /* Subnet Mask */
490 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000491#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500492#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000493 *e++ = 2;
494 *cnt += 1;
495#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500496#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000497 *e++ = 3; /* Router Option */
498 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000499#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500500#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000501 *e++ = 6; /* DNS Server(s) */
502 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000503#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500504#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000505 *e++ = 12; /* Hostname */
506 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000507#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500508#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000509 *e++ = 13; /* Boot File Size */
510 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000511#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500512#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000513 *e++ = 17; /* Boot path */
514 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000515#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500516#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000517 *e++ = 40; /* NIS Domain name request */
518 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000519#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500520#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000521 *e++ = 42;
522 *cnt += 1;
523#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800524 /* no options, so back up to avoid sending an empty request list */
525 if (*cnt == 0)
526 e -= 2;
527
wdenk232c1502004-03-12 00:14:09 +0000528 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000529
wdenk232c1502004-03-12 00:14:09 +0000530 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000531#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800532 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000533 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000534#endif
535
wdenk232c1502004-03-12 00:14:09 +0000536 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000537}
538
Jon Loeliger610f2e92007-07-10 11:05:02 -0500539#else
wdenk3861aa52002-09-27 23:19:37 +0000540/*
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500541 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000542 */
wdenk232c1502004-03-12 00:14:09 +0000543static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000544{
wdenk232c1502004-03-12 00:14:09 +0000545 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000546
wdenk232c1502004-03-12 00:14:09 +0000547 *e++ = 99; /* RFC1048 Magic Cookie */
548 *e++ = 130;
549 *e++ = 83;
550 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000551
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500552#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000553 *e++ = 53; /* DHCP Message Type */
554 *e++ = 1;
555 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000556
wdenk232c1502004-03-12 00:14:09 +0000557 *e++ = 57; /* Maximum DHCP Message Size */
558 *e++ = 2;
559 *e++ = (576 - 312 + OPT_SIZE) >> 16;
560 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500561#endif
wdenk3861aa52002-09-27 23:19:37 +0000562
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500563#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000564 *e++ = 1; /* Subnet mask request */
565 *e++ = 4;
566 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000567#endif
568
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500569#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000570 *e++ = 3; /* Default gateway request */
571 *e++ = 4;
572 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000573#endif
574
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500575#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000576 *e++ = 6; /* Domain Name Server */
577 *e++ = 4;
578 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000579#endif
580
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500581#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000582 *e++ = 12; /* Host name request */
583 *e++ = 32;
584 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000585#endif
586
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500587#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000588 *e++ = 13; /* Boot file size */
589 *e++ = 2;
590 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000591#endif
592
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500593#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000594 *e++ = 17; /* Boot path */
595 *e++ = 32;
596 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000597#endif
598
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500599#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000600 *e++ = 40; /* NIS Domain name request */
601 *e++ = 32;
602 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000603#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000604#if defined(CONFIG_BOOTP_NTPSERVER)
605 *e++ = 42;
606 *e++ = 4;
607 e += 4;
608#endif
wdenk3861aa52002-09-27 23:19:37 +0000609
wdenk232c1502004-03-12 00:14:09 +0000610 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000611
wdenk232c1502004-03-12 00:14:09 +0000612 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000613}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500614#endif
wdenk3861aa52002-09-27 23:19:37 +0000615
616void
617BootpRequest (void)
618{
619 volatile uchar *pkt, *iphdr;
620 Bootp_t *bp;
621 int ext_len, pktlen, iplen;
622
Simon Glass573f14f2011-12-10 11:08:06 +0000623 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500624#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000625 dhcp_state = INIT;
626#endif
627
628#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
629 unsigned char bi_enetaddr[6];
630 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000631 ulong tst1, tst2, sum, m_mask, m_value = 0;
632
633 if (BootpTry ==0) {
634 /* get our mac */
Mike Frysinger95823ca2009-02-11 18:23:48 -0500635 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000636
Robin Getz0ebf04c2009-07-23 03:01:03 -0400637 debug("BootpRequest => Our Mac: ");
638 for (reg=0; reg<6; reg++)
639 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000640
641 /* Mac-Manipulation 2 get seed1 */
642 tst1=0;
643 tst2=0;
644 for (reg=2; reg<6; reg++) {
645 tst1 = tst1 << 8;
646 tst1 = tst1 | bi_enetaddr[reg];
647 }
648 for (reg=0; reg<2; reg++) {
649 tst2 = tst2 | bi_enetaddr[reg];
650 tst2 = tst2 << 8;
651 }
652
653 seed1 = tst1^tst2;
654
655 /* Mirror seed1*/
656 m_mask=0x1;
657 for (reg=1;reg<=32;reg++) {
658 m_value |= (m_mask & seed1);
659 seed1 = seed1 >> 1;
660 m_value = m_value << 1;
661 }
662 seed1 = m_value;
663 seed2 = 0xB78D0945;
664 }
665
666 /* Random Number Generator */
667
668 for (reg=0;reg<=0;reg++) {
669 sum = seed1 + seed2;
670 if (sum < seed1 || sum < seed2)
671 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000672 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000673 seed1 = sum;
674
675 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
676 sum = sum >> (22-BootpTry);
677 } else { /*After 3rd BOOTP request max 8192 * 1ms */
678 sum = sum >> 19;
679 }
680 }
681
682 printf ("Random delay: %ld ms...\n", sum);
683 for (reg=0; reg <sum; reg++) {
684 udelay(1000); /*Wait 1ms*/
685 }
686#endif /* CONFIG_BOOTP_RANDOM_DELAY */
687
688 printf("BOOTP broadcast %d\n", ++BootpTry);
689 pkt = NetTxPacket;
690 memset ((void*)pkt, 0, PKTSIZE);
691
wdenka3d991b2004-04-15 21:48:45 +0000692 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000693
694 /*
695 * Next line results in incorrect packet size being transmitted, resulting
696 * in errors in some DHCP servers, reporting missing bytes. Size must be
697 * set in packet header after extension length has been determined.
698 * C. Hallinan, DS4.COM, Inc.
699 */
700 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
701 iphdr = pkt; /* We need this later for NetSetIP() */
702 pkt += IP_HDR_SIZE;
703
704 bp = (Bootp_t *)pkt;
705 bp->bp_op = OP_BOOTREQUEST;
706 bp->bp_htype = HWT_ETHER;
707 bp->bp_hlen = HWL_ETHER;
708 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200709 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000710 NetWriteIP(&bp->bp_ciaddr, 0);
711 NetWriteIP(&bp->bp_yiaddr, 0);
712 NetWriteIP(&bp->bp_siaddr, 0);
713 NetWriteIP(&bp->bp_giaddr, 0);
714 memcpy (bp->bp_chaddr, NetOurEther, 6);
715 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
716
717 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500718#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200719 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000720#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200721 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500722#endif
wdenk3861aa52002-09-27 23:19:37 +0000723
724 /*
725 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200726 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000727 */
728 BootpID = ((ulong)NetOurEther[2] << 24)
729 | ((ulong)NetOurEther[3] << 16)
730 | ((ulong)NetOurEther[4] << 8)
731 | (ulong)NetOurEther[5];
732 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000733 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000734 NetCopyLong(&bp->bp_id, &BootpID);
735
736 /*
737 * Calculate proper packet lengths taking into account the
738 * variable size of the options field
739 */
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200740 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000741 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
742 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200743 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000744
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500745#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000746 dhcp_state = SELECTING;
747 NetSetHandler(DhcpHandler);
748#else
749 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500750#endif
wdenk3861aa52002-09-27 23:19:37 +0000751 NetSendPacket(NetTxPacket, pktlen);
752}
753
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500754#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100755static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000756{
wdenk3e386912003-04-05 00:53:31 +0000757 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000758 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200759#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
760 int *to_ptr;
761#endif
wdenk3861aa52002-09-27 23:19:37 +0000762
Justin L Wernerdc80e002012-08-09 14:20:30 -0600763#if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED)
764 dhcp_vendorex_opts_done();
765#endif
wdenk232c1502004-03-12 00:14:09 +0000766 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000767 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000768 switch (*popt) {
769 case 1:
770 NetCopyIP (&NetOurSubnetMask, (popt + 2));
771 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500772#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000773 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200774 to_ptr = &NetTimeOffset;
775 NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000776 NetTimeOffset = ntohl (NetTimeOffset);
777 break;
778#endif
wdenk232c1502004-03-12 00:14:09 +0000779 case 3:
780 NetCopyIP (&NetOurGatewayIP, (popt + 2));
781 break;
782 case 6:
783 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500784#if defined(CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000785 if (*(popt + 1) > 4) {
786 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
787 }
stroesefe389a82003-08-28 14:17:32 +0000788#endif
wdenk232c1502004-03-12 00:14:09 +0000789 break;
790 case 12:
791 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
792 memcpy (&NetOurHostName, popt + 2, size);
793 NetOurHostName[size] = 0;
794 break;
795 case 15: /* Ignore Domain Name Option */
796 break;
797 case 17:
798 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
799 memcpy (&NetOurRootPath, popt + 2, size);
800 NetOurRootPath[size] = 0;
801 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500802#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000803 case 42: /* NTP server IP */
804 NetCopyIP (&NetNtpServerIP, (popt + 2));
805 break;
806#endif
wdenk232c1502004-03-12 00:14:09 +0000807 case 51:
808 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
809 break;
810 case 53: /* Ignore Message Type Option */
811 break;
812 case 54:
813 NetCopyIP (&NetDHCPServerIP, (popt + 2));
814 break;
815 case 58: /* Ignore Renewal Time Option */
816 break;
817 case 59: /* Ignore Rebinding Time Option */
818 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100819 case 66: /* Ignore TFTP server name */
820 break;
821 case 67: /* vendor opt bootfile */
822 /*
823 * I can't use dhcp_vendorex_proc here because I need
824 * to write into the bootp packet - even then I had to
825 * pass the bootp packet pointer into here as the
826 * second arg
827 */
828 size = truncate_sz ("Opt Boot File",
829 sizeof(bp->bp_file),
830 oplen);
831 if (bp->bp_file[0] == '\0' && size > 0) {
832 /*
833 * only use vendor boot file if we didn't
834 * receive a boot file in the main non-vendor
835 * part of the packet - god only knows why
836 * some vendors chose not to use this perfectly
837 * good spot to store the boot file (join on
838 * Tru64 Unix) it seems mind bogglingly crazy
839 * to me
840 */
841 printf("*** WARNING: using vendor "
842 "optional boot file\n");
843 memcpy(bp->bp_file, popt + 2, size);
844 bp->bp_file[size] = '\0';
845 }
846 break;
wdenk232c1502004-03-12 00:14:09 +0000847 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500848#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000849 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000850 break;
wdenk3861aa52002-09-27 23:19:37 +0000851#endif
wdenk232c1502004-03-12 00:14:09 +0000852 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
853 break;
wdenk3861aa52002-09-27 23:19:37 +0000854 }
855 popt += oplen + 2; /* Process next option */
856 }
Justin L Wernerdc80e002012-08-09 14:20:30 -0600857#if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED)
858 dhcp_vendorex_opts_done();
859#endif
wdenk3861aa52002-09-27 23:19:37 +0000860}
861
862static int DhcpMessageType(unsigned char *popt)
863{
864 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
865 return -1;
866
867 popt += 4;
868 while ( *popt != 0xff ) {
869 if ( *popt == 53 ) /* DHCP Message Type */
870 return *(popt + 2);
871 popt += *(popt + 1) + 2; /* Scan through all options */
872 }
873 return -1;
874}
875
wdenk3e386912003-04-05 00:53:31 +0000876static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000877{
878 volatile uchar *pkt, *iphdr;
879 Bootp_t *bp;
880 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000881 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000882
Robin Getz0ebf04c2009-07-23 03:01:03 -0400883 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000884 pkt = NetTxPacket;
885 memset ((void*)pkt, 0, PKTSIZE);
886
wdenka3d991b2004-04-15 21:48:45 +0000887 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000888
889 iphdr = pkt; /* We'll need this later to set proper pkt size */
890 pkt += IP_HDR_SIZE;
891
892 bp = (Bootp_t *)pkt;
893 bp->bp_op = OP_BOOTREQUEST;
894 bp->bp_htype = HWT_ETHER;
895 bp->bp_hlen = HWL_ETHER;
896 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200897 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammiae5c794e2007-10-29 17:40:35 -0400898 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
899 * the server yet */
900
Wolfgang Denkc6686702006-10-12 00:01:08 +0200901 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200902 * RFC3046 requires Relay Agents to discard packets with
903 * nonzero and offered giaddr
904 */
905 NetWriteIP(&bp->bp_giaddr, 0);
906
wdenk3861aa52002-09-27 23:19:37 +0000907 memcpy (bp->bp_chaddr, NetOurEther, 6);
908
909 /*
910 * ID is the id of the OFFER packet
911 */
912
913 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
914
915 /*
916 * Copy options from OFFER packet if present
917 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400918
919 /* Copy offered IP into the parameters request list */
920 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200921 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000922
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200923 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000924 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
925 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
926
Robin Getz0ebf04c2009-07-23 03:01:03 -0400927 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100928#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
929 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
930#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000931 NetSendPacket(NetTxPacket, pktlen);
932}
933
934/*
935 * Handle DHCP received packets.
936 */
937static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000938DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
939 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000940{
941 Bootp_t *bp = (Bootp_t *)pkt;
942
Robin Getz0ebf04c2009-07-23 03:01:03 -0400943 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000944 src, dest, len, dhcp_state);
945
wdenk232c1502004-03-12 00:14:09 +0000946 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000947 return;
948
Robin Getz0ebf04c2009-07-23 03:01:03 -0400949 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000950 src, dest, len, dhcp_state);
951
952 switch (dhcp_state) {
953 case SELECTING:
954 /*
955 * Wait an appropriate time for any potential DHCPOFFER packets
956 * to arrive. Then select one, and generate DHCPREQUEST response.
957 * If filename is in format we recognize, assume it is a valid
958 * OFFER from a server we want.
959 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400960 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200961#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000962 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200963 CONFIG_SYS_BOOTFILE_PREFIX,
964 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
965#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000966
Robin Getz0ebf04c2009-07-23 03:01:03 -0400967 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000968 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000969
wdenk3861aa52002-09-27 23:19:37 +0000970 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100971 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000972
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200973 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000974 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200975#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000976 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200977#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000978
979 return;
980 break;
981 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400982 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000983
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200984 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000985 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100986 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk232c1502004-03-12 00:14:09 +0000987 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000988 dhcp_state = BOUND;
Mike Frysingerb6446b62009-02-17 00:00:53 -0500989 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000990 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
991 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000992
Simon Glasse4a3d572011-10-27 06:24:32 +0000993 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000994 return;
995 }
996 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200997 case BOUND:
998 /* DHCP client bound to address */
999 break;
wdenk3861aa52002-09-27 23:19:37 +00001000 default:
wdenk4b9206e2004-03-23 22:14:11 +00001001 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001002 break;
1003 }
1004
1005}
1006
1007void DhcpRequest(void)
1008{
1009 BootpRequest();
1010}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001011#endif /* CONFIG_CMD_DHCP */