blob: b789eec0d7edc3966da5197c1f7bbe5a330ce609 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk232c1502004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
12#include <command.h>
13#include <net.h>
14#include "bootp.h"
15#include "tftp.h"
wdenk232c1502004-03-12 00:14:09 +000016#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000017#ifdef CONFIG_STATUS_LED
18#include <status_led.h>
19#endif
20
wdenk232c1502004-03-12 00:14:09 +000021#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000022
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020023#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000024#ifndef CONFIG_NET_RETRY_COUNT
wdenk3861aa52002-09-27 23:19:37 +000025# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
26#else
wdenk232c1502004-03-12 00:14:09 +000027# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000028#endif
29
30#define PORT_BOOTPS 67 /* BOOTP server UDP port */
31#define PORT_BOOTPC 68 /* BOOTP client UDP port */
32
33#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000034#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000035#endif
36
37ulong BootpID;
38int BootpTry;
39#ifdef CONFIG_BOOTP_RANDOM_DELAY
40ulong seed1, seed2;
41#endif
42
Jon Loeliger643d1ab2007-07-09 17:45:14 -050043#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000044dhcp_state_t dhcp_state = INIT;
wdenk682011f2003-06-03 23:54:09 +000045unsigned long dhcp_leasetime = 0;
stroese759a51b2003-04-10 13:26:44 +000046IPaddr_t NetDHCPServerIP = 0;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000047static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
48 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000049
50/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000051#if 0
52static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000053{
54 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000055 case 1: return "DHCPDISCOVER"; break;
56 case 2: return "DHCPOFFER"; break;
57 case 3: return "DHCPREQUEST"; break;
58 case 4: return "DHCPDECLINE"; break;
59 case 5: return "DHCPACK"; break;
60 case 6: return "DHCPNACK"; break;
61 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000062 default: return "UNKNOWN/INVALID MSG TYPE"; break;
63 }
64}
wdenk3e386912003-04-05 00:53:31 +000065#endif
wdenk3861aa52002-09-27 23:19:37 +000066
Jon Loeliger1fe80d72007-07-09 22:08:34 -050067#if defined(CONFIG_BOOTP_VENDOREX)
wdenk3861aa52002-09-27 23:19:37 +000068extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
69extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
70#endif
71
Jon Loeliger610f2e92007-07-10 11:05:02 -050072#endif
wdenk3861aa52002-09-27 23:19:37 +000073
74static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
75{
76 Bootp_t *bp = (Bootp_t *) pkt;
77 int retval = 0;
78
79 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
80 retval = -1;
81 else if (len < sizeof (Bootp_t) - OPT_SIZE)
82 retval = -2;
83 else if (bp->bp_op != OP_BOOTREQUEST &&
84 bp->bp_op != OP_BOOTREPLY &&
85 bp->bp_op != DHCP_OFFER &&
86 bp->bp_op != DHCP_ACK &&
87 bp->bp_op != DHCP_NAK ) {
88 retval = -3;
89 }
90 else if (bp->bp_htype != HWT_ETHER)
91 retval = -4;
92 else if (bp->bp_hlen != HWL_ETHER)
93 retval = -5;
94 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
95 retval = -6;
96 }
97
Robin Getz0ebf04c2009-07-23 03:01:03 -040098 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000099
100 return retval;
101}
102
103/*
104 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
105 */
wdenk3e386912003-04-05 00:53:31 +0000106static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000107{
wdenk3d3befa2004-03-14 15:06:13 +0000108 IPaddr_t tmp_ip;
109
wdenk3861aa52002-09-27 23:19:37 +0000110 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400111#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000112 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
113 if (tmp_ip != 0)
114 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400115 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400116#endif
wdenk3d3befa2004-03-14 15:06:13 +0000117 if (strlen(bp->bp_file) > 0)
118 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000119
Robin Getz0ebf04c2009-07-23 03:01:03 -0400120 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000121
122 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000123 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000124 * not contain a new value
125 */
126 if (*BootFile) {
127 setenv ("bootfile", BootFile);
128 }
129}
130
131static int truncate_sz (const char *name, int maxlen, int curlen)
132{
133 if (curlen >= maxlen) {
134 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
135 name, curlen, maxlen);
136 curlen = maxlen - 1;
137 }
138 return (curlen);
139}
140
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500141#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000142
wdenk232c1502004-03-12 00:14:09 +0000143static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000144{
wdenk232c1502004-03-12 00:14:09 +0000145 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000146
Robin Getz0ebf04c2009-07-23 03:01:03 -0400147 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
wdenk232c1502004-03-12 00:14:09 +0000148 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000149
wdenk232c1502004-03-12 00:14:09 +0000150 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000151
wdenk232c1502004-03-12 00:14:09 +0000152 switch (*ext) {
153 /* Fixed length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700154 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000155 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000156 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000157 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700158 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000159 break;
wdenk232c1502004-03-12 00:14:09 +0000160 /* Variable length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700161 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000162 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000163 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000164 }
165 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700166 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000167 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700168 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000169 break;
170 case 6:
171 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000172 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000173 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500174#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000175 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000176 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000177 }
178#endif
wdenk3861aa52002-09-27 23:19:37 +0000179 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700180 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000181 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700182 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000183 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700184 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000185 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700186 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000187 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700188 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000189 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700190 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000191 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000192 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
193 memcpy (&NetOurHostName, ext + 2, size);
194 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000195 }
196 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700197 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000198 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000199 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000200 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000201 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000202 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700203 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000204 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700205 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000206 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700207 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000208 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700209 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000210 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000211 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
212 memcpy (&NetOurRootPath, ext + 2, size);
213 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000214 }
215 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700216 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000217 /*
wdenk8bde7f72003-06-27 21:31:46 +0000218 * This can be used to send the information of the
219 * vendor area in another file that the client can
220 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000221 */
222 break;
wdenk232c1502004-03-12 00:14:09 +0000223 /* IP host layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700224 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000225 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000226 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
227 memcpy (&NetOurNISDomain, ext + 2, size);
228 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000229 }
230 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000231#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
232 case 42: /* NTP server IP */
233 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
234 break;
235#endif
wdenk232c1502004-03-12 00:14:09 +0000236 /* Application layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700237 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
Robin Getz0ebf04c2009-07-23 03:01:03 -0400251 debug("[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
Robin Getz0ebf04c2009-07-23 03:01:03 -0400265 debug("[BOOTP] Received fields: \n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500266 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400267 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000268
Mike Frysingerb6446b62009-02-17 00:00:53 -0500269 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400270 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000271
Robin Getz0ebf04c2009-07-23 03:01:03 -0400272 if (NetBootFileSize)
273 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000274
Robin Getz0ebf04c2009-07-23 03:01:03 -0400275 if (NetOurHostName[0])
276 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000277
Robin Getz0ebf04c2009-07-23 03:01:03 -0400278 if (NetOurRootPath[0])
279 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000280
Robin Getz0ebf04c2009-07-23 03:01:03 -0400281 if (NetOurNISDomain[0])
282 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000283
Robin Getz0ebf04c2009-07-23 03:01:03 -0400284 if (NetBootFileSize)
285 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000286
287#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
288 if (NetNtpServerIP)
289 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
290#endif
wdenk3861aa52002-09-27 23:19:37 +0000291}
Simon Glass09349862011-06-13 16:13:12 -0700292
wdenk3861aa52002-09-27 23:19:37 +0000293/*
294 * Handle a BOOTP received packet.
295 */
296static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000297BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
298 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000299{
300 Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000301
Robin Getz0ebf04c2009-07-23 03:01:03 -0400302 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000303 src, dest, len, sizeof (Bootp_t));
304
305 bp = (Bootp_t *)pkt;
306
wdenk232c1502004-03-12 00:14:09 +0000307 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000308 return;
309
310 /*
wdenk232c1502004-03-12 00:14:09 +0000311 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000312 */
313#ifdef CONFIG_STATUS_LED
314 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
315#endif
316
317 BootpCopyNetParams(bp); /* Store net parameters from reply */
318
319 /* Retrieve extended information (we must parse the vendor area) */
320 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200321 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000322
323 NetSetTimeout(0, (thand_f *)0);
324
Robin Getz0ebf04c2009-07-23 03:01:03 -0400325 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000326
Simon Glasse4a3d572011-10-27 06:24:32 +0000327 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000328}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500329#endif
wdenk3861aa52002-09-27 23:19:37 +0000330
331/*
332 * Timeout on BOOTP/DHCP request.
333 */
334static void
335BootpTimeout(void)
336{
337 if (BootpTry >= TIMEOUT_COUNT) {
338 puts ("\nRetry count exceeded; starting again\n");
339 NetStartAgain ();
340 } else {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200341 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000342 BootpRequest ();
343 }
344}
345
346/*
347 * Initialize BOOTP extension fields in the request.
348 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500349#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000350static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000351{
wdenk232c1502004-03-12 00:14:09 +0000352 u8 *start = e;
353 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000354#if defined(CONFIG_BOOTP_PXE)
355 char *uuid;
356 size_t vci_strlen;
357 u16 clientarch;
358#endif
wdenk232c1502004-03-12 00:14:09 +0000359
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500360#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000361 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000362#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500363#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200364 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000365#endif
wdenk3861aa52002-09-27 23:19:37 +0000366
wdenk232c1502004-03-12 00:14:09 +0000367 *e++ = 99; /* RFC1048 Magic Cookie */
368 *e++ = 130;
369 *e++ = 83;
370 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000371
wdenk232c1502004-03-12 00:14:09 +0000372 *e++ = 53; /* DHCP Message Type */
373 *e++ = 1;
374 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000375
wdenk232c1502004-03-12 00:14:09 +0000376 *e++ = 57; /* Maximum DHCP Message Size */
377 *e++ = 2;
378 *e++ = (576 - 312 + OPT_SIZE) >> 8;
379 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000380
wdenk232c1502004-03-12 00:14:09 +0000381 if (ServerID) {
382 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000383
wdenk232c1502004-03-12 00:14:09 +0000384 *e++ = 54; /* ServerID */
385 *e++ = 4;
386 *e++ = tmp >> 24;
387 *e++ = tmp >> 16;
388 *e++ = tmp >> 8;
389 *e++ = tmp & 0xff;
390 }
wdenk3861aa52002-09-27 23:19:37 +0000391
wdenk232c1502004-03-12 00:14:09 +0000392 if (RequestedIP) {
393 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000394
wdenk232c1502004-03-12 00:14:09 +0000395 *e++ = 50; /* Requested IP */
396 *e++ = 4;
397 *e++ = tmp >> 24;
398 *e++ = tmp >> 16;
399 *e++ = tmp >> 8;
400 *e++ = tmp & 0xff;
401 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500402#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000403 if ((hostname = getenv ("hostname"))) {
404 int hostnamelen = strlen (hostname);
405
406 *e++ = 12; /* Hostname */
407 *e++ = hostnamelen;
408 memcpy (e, hostname, hostnamelen);
409 e += hostnamelen;
410 }
stroesefe389a82003-08-28 14:17:32 +0000411#endif
412
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000413#if defined(CONFIG_BOOTP_PXE)
414 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
415 *e++ = 93; /* Client System Architecture */
416 *e++ = 2;
417 *e++ = (clientarch >> 8) & 0xff;
418 *e++ = clientarch & 0xff;
419
420 *e++ = 94; /* Client Network Interface Identifier */
421 *e++ = 3;
422 *e++ = 1; /* type field for UNDI */
423 *e++ = 0; /* major revision */
424 *e++ = 0; /* minor revision */
425
426 uuid = getenv("pxeuuid");
427
428 if (uuid) {
429 if (uuid_str_valid(uuid)) {
430 *e++ = 97; /* Client Machine Identifier */
431 *e++ = 17;
432 *e++ = 0; /* type 0 - UUID */
433
434 uuid_str_to_bin(uuid, e);
435 e += 16;
436 } else {
437 printf("Invalid pxeuuid: %s\n", uuid);
438 }
439 }
440
441 *e++ = 60; /* Vendor Class Identifier */
442 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
443 *e++ = vci_strlen;
444 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
445 e += vci_strlen;
446#endif
447
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500448#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000449 if ((x = dhcp_vendorex_prep (e)))
450 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000451#endif
452
wdenk232c1502004-03-12 00:14:09 +0000453 *e++ = 55; /* Parameter Request List */
454 cnt = e++; /* Pointer to count of requested items */
455 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500456#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000457 *e++ = 1; /* Subnet Mask */
458 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000459#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500460#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000461 *e++ = 2;
462 *cnt += 1;
463#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500464#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000465 *e++ = 3; /* Router Option */
466 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000467#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500468#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000469 *e++ = 6; /* DNS Server(s) */
470 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000471#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500472#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000473 *e++ = 12; /* Hostname */
474 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000475#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500476#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000477 *e++ = 13; /* Boot File Size */
478 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000479#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500480#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000481 *e++ = 17; /* Boot path */
482 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000483#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500484#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000485 *e++ = 40; /* NIS Domain name request */
486 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000487#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500488#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000489 *e++ = 42;
490 *cnt += 1;
491#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800492 /* no options, so back up to avoid sending an empty request list */
493 if (*cnt == 0)
494 e -= 2;
495
wdenk232c1502004-03-12 00:14:09 +0000496 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000497
wdenk232c1502004-03-12 00:14:09 +0000498 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000499#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800500 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000501 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000502#endif
503
wdenk232c1502004-03-12 00:14:09 +0000504 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000505}
506
Jon Loeliger610f2e92007-07-10 11:05:02 -0500507#else
wdenk3861aa52002-09-27 23:19:37 +0000508/*
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500509 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000510 */
wdenk232c1502004-03-12 00:14:09 +0000511static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000512{
wdenk232c1502004-03-12 00:14:09 +0000513 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000514
wdenk232c1502004-03-12 00:14:09 +0000515 *e++ = 99; /* RFC1048 Magic Cookie */
516 *e++ = 130;
517 *e++ = 83;
518 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000519
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500520#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000521 *e++ = 53; /* DHCP Message Type */
522 *e++ = 1;
523 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000524
wdenk232c1502004-03-12 00:14:09 +0000525 *e++ = 57; /* Maximum DHCP Message Size */
526 *e++ = 2;
527 *e++ = (576 - 312 + OPT_SIZE) >> 16;
528 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500529#endif
wdenk3861aa52002-09-27 23:19:37 +0000530
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500531#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000532 *e++ = 1; /* Subnet mask request */
533 *e++ = 4;
534 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000535#endif
536
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500537#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000538 *e++ = 3; /* Default gateway request */
539 *e++ = 4;
540 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000541#endif
542
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500543#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000544 *e++ = 6; /* Domain Name Server */
545 *e++ = 4;
546 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000547#endif
548
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500549#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000550 *e++ = 12; /* Host name request */
551 *e++ = 32;
552 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000553#endif
554
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500555#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000556 *e++ = 13; /* Boot file size */
557 *e++ = 2;
558 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000559#endif
560
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500561#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000562 *e++ = 17; /* Boot path */
563 *e++ = 32;
564 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000565#endif
566
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500567#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000568 *e++ = 40; /* NIS Domain name request */
569 *e++ = 32;
570 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000571#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000572#if defined(CONFIG_BOOTP_NTPSERVER)
573 *e++ = 42;
574 *e++ = 4;
575 e += 4;
576#endif
wdenk3861aa52002-09-27 23:19:37 +0000577
wdenk232c1502004-03-12 00:14:09 +0000578 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000579
wdenk232c1502004-03-12 00:14:09 +0000580 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000581}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500582#endif
wdenk3861aa52002-09-27 23:19:37 +0000583
584void
585BootpRequest (void)
586{
587 volatile uchar *pkt, *iphdr;
588 Bootp_t *bp;
589 int ext_len, pktlen, iplen;
590
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500591#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000592 dhcp_state = INIT;
593#endif
594
595#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
596 unsigned char bi_enetaddr[6];
597 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000598 ulong tst1, tst2, sum, m_mask, m_value = 0;
599
600 if (BootpTry ==0) {
601 /* get our mac */
Mike Frysinger95823ca2009-02-11 18:23:48 -0500602 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000603
Robin Getz0ebf04c2009-07-23 03:01:03 -0400604 debug("BootpRequest => Our Mac: ");
605 for (reg=0; reg<6; reg++)
606 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000607
608 /* Mac-Manipulation 2 get seed1 */
609 tst1=0;
610 tst2=0;
611 for (reg=2; reg<6; reg++) {
612 tst1 = tst1 << 8;
613 tst1 = tst1 | bi_enetaddr[reg];
614 }
615 for (reg=0; reg<2; reg++) {
616 tst2 = tst2 | bi_enetaddr[reg];
617 tst2 = tst2 << 8;
618 }
619
620 seed1 = tst1^tst2;
621
622 /* Mirror seed1*/
623 m_mask=0x1;
624 for (reg=1;reg<=32;reg++) {
625 m_value |= (m_mask & seed1);
626 seed1 = seed1 >> 1;
627 m_value = m_value << 1;
628 }
629 seed1 = m_value;
630 seed2 = 0xB78D0945;
631 }
632
633 /* Random Number Generator */
634
635 for (reg=0;reg<=0;reg++) {
636 sum = seed1 + seed2;
637 if (sum < seed1 || sum < seed2)
638 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000639 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000640 seed1 = sum;
641
642 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
643 sum = sum >> (22-BootpTry);
644 } else { /*After 3rd BOOTP request max 8192 * 1ms */
645 sum = sum >> 19;
646 }
647 }
648
649 printf ("Random delay: %ld ms...\n", sum);
650 for (reg=0; reg <sum; reg++) {
651 udelay(1000); /*Wait 1ms*/
652 }
653#endif /* CONFIG_BOOTP_RANDOM_DELAY */
654
655 printf("BOOTP broadcast %d\n", ++BootpTry);
656 pkt = NetTxPacket;
657 memset ((void*)pkt, 0, PKTSIZE);
658
wdenka3d991b2004-04-15 21:48:45 +0000659 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000660
661 /*
662 * Next line results in incorrect packet size being transmitted, resulting
663 * in errors in some DHCP servers, reporting missing bytes. Size must be
664 * set in packet header after extension length has been determined.
665 * C. Hallinan, DS4.COM, Inc.
666 */
667 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
668 iphdr = pkt; /* We need this later for NetSetIP() */
669 pkt += IP_HDR_SIZE;
670
671 bp = (Bootp_t *)pkt;
672 bp->bp_op = OP_BOOTREQUEST;
673 bp->bp_htype = HWT_ETHER;
674 bp->bp_hlen = HWL_ETHER;
675 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200676 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000677 NetWriteIP(&bp->bp_ciaddr, 0);
678 NetWriteIP(&bp->bp_yiaddr, 0);
679 NetWriteIP(&bp->bp_siaddr, 0);
680 NetWriteIP(&bp->bp_giaddr, 0);
681 memcpy (bp->bp_chaddr, NetOurEther, 6);
682 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
683
684 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500685#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200686 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000687#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200688 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500689#endif
wdenk3861aa52002-09-27 23:19:37 +0000690
691 /*
692 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200693 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000694 */
695 BootpID = ((ulong)NetOurEther[2] << 24)
696 | ((ulong)NetOurEther[3] << 16)
697 | ((ulong)NetOurEther[4] << 8)
698 | (ulong)NetOurEther[5];
699 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000700 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000701 NetCopyLong(&bp->bp_id, &BootpID);
702
703 /*
704 * Calculate proper packet lengths taking into account the
705 * variable size of the options field
706 */
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200707 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000708 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
709 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200710 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000711
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500712#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000713 dhcp_state = SELECTING;
714 NetSetHandler(DhcpHandler);
715#else
716 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500717#endif
wdenk3861aa52002-09-27 23:19:37 +0000718 NetSendPacket(NetTxPacket, pktlen);
719}
720
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500721#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100722static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000723{
wdenk3e386912003-04-05 00:53:31 +0000724 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000725 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200726#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
727 int *to_ptr;
728#endif
wdenk3861aa52002-09-27 23:19:37 +0000729
wdenk232c1502004-03-12 00:14:09 +0000730 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000731 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000732 switch (*popt) {
733 case 1:
734 NetCopyIP (&NetOurSubnetMask, (popt + 2));
735 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500736#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000737 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200738 to_ptr = &NetTimeOffset;
739 NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000740 NetTimeOffset = ntohl (NetTimeOffset);
741 break;
742#endif
wdenk232c1502004-03-12 00:14:09 +0000743 case 3:
744 NetCopyIP (&NetOurGatewayIP, (popt + 2));
745 break;
746 case 6:
747 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500748#if defined(CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000749 if (*(popt + 1) > 4) {
750 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
751 }
stroesefe389a82003-08-28 14:17:32 +0000752#endif
wdenk232c1502004-03-12 00:14:09 +0000753 break;
754 case 12:
755 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
756 memcpy (&NetOurHostName, popt + 2, size);
757 NetOurHostName[size] = 0;
758 break;
759 case 15: /* Ignore Domain Name Option */
760 break;
761 case 17:
762 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
763 memcpy (&NetOurRootPath, popt + 2, size);
764 NetOurRootPath[size] = 0;
765 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500766#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000767 case 42: /* NTP server IP */
768 NetCopyIP (&NetNtpServerIP, (popt + 2));
769 break;
770#endif
wdenk232c1502004-03-12 00:14:09 +0000771 case 51:
772 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
773 break;
774 case 53: /* Ignore Message Type Option */
775 break;
776 case 54:
777 NetCopyIP (&NetDHCPServerIP, (popt + 2));
778 break;
779 case 58: /* Ignore Renewal Time Option */
780 break;
781 case 59: /* Ignore Rebinding Time Option */
782 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100783 case 66: /* Ignore TFTP server name */
784 break;
785 case 67: /* vendor opt bootfile */
786 /*
787 * I can't use dhcp_vendorex_proc here because I need
788 * to write into the bootp packet - even then I had to
789 * pass the bootp packet pointer into here as the
790 * second arg
791 */
792 size = truncate_sz ("Opt Boot File",
793 sizeof(bp->bp_file),
794 oplen);
795 if (bp->bp_file[0] == '\0' && size > 0) {
796 /*
797 * only use vendor boot file if we didn't
798 * receive a boot file in the main non-vendor
799 * part of the packet - god only knows why
800 * some vendors chose not to use this perfectly
801 * good spot to store the boot file (join on
802 * Tru64 Unix) it seems mind bogglingly crazy
803 * to me
804 */
805 printf("*** WARNING: using vendor "
806 "optional boot file\n");
807 memcpy(bp->bp_file, popt + 2, size);
808 bp->bp_file[size] = '\0';
809 }
810 break;
wdenk232c1502004-03-12 00:14:09 +0000811 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500812#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000813 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000814 break;
wdenk3861aa52002-09-27 23:19:37 +0000815#endif
wdenk232c1502004-03-12 00:14:09 +0000816 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
817 break;
wdenk3861aa52002-09-27 23:19:37 +0000818 }
819 popt += oplen + 2; /* Process next option */
820 }
821}
822
823static int DhcpMessageType(unsigned char *popt)
824{
825 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
826 return -1;
827
828 popt += 4;
829 while ( *popt != 0xff ) {
830 if ( *popt == 53 ) /* DHCP Message Type */
831 return *(popt + 2);
832 popt += *(popt + 1) + 2; /* Scan through all options */
833 }
834 return -1;
835}
836
wdenk3e386912003-04-05 00:53:31 +0000837static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000838{
839 volatile uchar *pkt, *iphdr;
840 Bootp_t *bp;
841 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000842 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000843
Robin Getz0ebf04c2009-07-23 03:01:03 -0400844 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000845 pkt = NetTxPacket;
846 memset ((void*)pkt, 0, PKTSIZE);
847
wdenka3d991b2004-04-15 21:48:45 +0000848 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000849
850 iphdr = pkt; /* We'll need this later to set proper pkt size */
851 pkt += IP_HDR_SIZE;
852
853 bp = (Bootp_t *)pkt;
854 bp->bp_op = OP_BOOTREQUEST;
855 bp->bp_htype = HWT_ETHER;
856 bp->bp_hlen = HWL_ETHER;
857 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200858 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammiae5c794e2007-10-29 17:40:35 -0400859 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
860 * the server yet */
861
Wolfgang Denkc6686702006-10-12 00:01:08 +0200862 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200863 * RFC3046 requires Relay Agents to discard packets with
864 * nonzero and offered giaddr
865 */
866 NetWriteIP(&bp->bp_giaddr, 0);
867
wdenk3861aa52002-09-27 23:19:37 +0000868 memcpy (bp->bp_chaddr, NetOurEther, 6);
869
870 /*
871 * ID is the id of the OFFER packet
872 */
873
874 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
875
876 /*
877 * Copy options from OFFER packet if present
878 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400879
880 /* Copy offered IP into the parameters request list */
881 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200882 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000883
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200884 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000885 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
886 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
887
Robin Getz0ebf04c2009-07-23 03:01:03 -0400888 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100889#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
890 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
891#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000892 NetSendPacket(NetTxPacket, pktlen);
893}
894
895/*
896 * Handle DHCP received packets.
897 */
898static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000899DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
900 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000901{
902 Bootp_t *bp = (Bootp_t *)pkt;
903
Robin Getz0ebf04c2009-07-23 03:01:03 -0400904 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000905 src, dest, len, dhcp_state);
906
wdenk232c1502004-03-12 00:14:09 +0000907 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000908 return;
909
Robin Getz0ebf04c2009-07-23 03:01:03 -0400910 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000911 src, dest, len, dhcp_state);
912
913 switch (dhcp_state) {
914 case SELECTING:
915 /*
916 * Wait an appropriate time for any potential DHCPOFFER packets
917 * to arrive. Then select one, and generate DHCPREQUEST response.
918 * If filename is in format we recognize, assume it is a valid
919 * OFFER from a server we want.
920 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400921 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200922#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000923 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200924 CONFIG_SYS_BOOTFILE_PREFIX,
925 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
926#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000927
Robin Getz0ebf04c2009-07-23 03:01:03 -0400928 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000929 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000930
wdenk3861aa52002-09-27 23:19:37 +0000931 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100932 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000933
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200934 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000935 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200936#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000937 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200938#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000939
940 return;
941 break;
942 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400943 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000944
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200945 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000946 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100947 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk232c1502004-03-12 00:14:09 +0000948 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000949 dhcp_state = BOUND;
Mike Frysingerb6446b62009-02-17 00:00:53 -0500950 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
wdenk3861aa52002-09-27 23:19:37 +0000951
Simon Glasse4a3d572011-10-27 06:24:32 +0000952 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000953 return;
954 }
955 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200956 case BOUND:
957 /* DHCP client bound to address */
958 break;
wdenk3861aa52002-09-27 23:19:37 +0000959 default:
wdenk4b9206e2004-03-23 22:14:11 +0000960 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000961 break;
962 }
963
964}
965
966void DhcpRequest(void)
967{
968 BootpRequest();
969}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100970#endif /* CONFIG_CMD_DHCP */