blob: 189a00383543982c448d9d6713725fe32c1ba86b [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
wdenk29e7f5a2004-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"
wdenk29e7f5a2004-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
Kim Phillips576175b2012-07-05 13:19:32 +000020#ifdef CONFIG_BOOTP_RANDOM_DELAY
21#include "net_rand.h"
22#endif
wdenk3861aa52002-09-27 23:19:37 +000023
Joe Hershberger8f4b1352012-05-15 08:59:06 +000024#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000025
Bartlomiej Sieka56668462008-10-01 15:26:28 +020026#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk29e7f5a2004-03-12 00:14:09 +000027#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger8f4b1352012-05-15 08:59:06 +000028# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000029#else
wdenk29e7f5a2004-03-12 00:14:09 +000030# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000031#endif
32
Joe Hershberger8f4b1352012-05-15 08:59:06 +000033#define PORT_BOOTPS 67 /* BOOTP server UDP port */
34#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000035
Joe Hershberger8f4b1352012-05-15 08:59:06 +000036#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk29e7f5a2004-03-12 00:14:09 +000037#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000038#endif
39
40ulong BootpID;
41int BootpTry;
wdenk3861aa52002-09-27 23:19:37 +000042
Jon Loeliger54f35c22007-07-09 17:45:14 -050043#if defined(CONFIG_CMD_DHCP)
Kim Phillips40c2c032012-10-29 13:34:33 +000044static dhcp_state_t dhcp_state = INIT;
45static unsigned long dhcp_leasetime;
46static IPaddr_t NetDHCPServerIP;
Luca Ceresoli428ab362011-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 */
wdenkb02744a2003-04-05 00:53:31 +000051#if 0
52static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000053{
54 switch (type) {
wdenk29e7f5a2004-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}
wdenkb02744a2003-04-05 00:53:31 +000065#endif
Jon Loeligera9807e52007-07-10 11:05:02 -050066#endif
wdenk3861aa52002-09-27 23:19:37 +000067
68static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
69{
Joe Hershberger8f4b1352012-05-15 08:59:06 +000070 struct Bootp_t *bp = (struct Bootp_t *) pkt;
wdenk3861aa52002-09-27 23:19:37 +000071 int retval = 0;
72
73 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
74 retval = -1;
Joe Hershbergerceba4472012-05-23 07:58:14 +000075 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +000076 retval = -2;
77 else if (bp->bp_op != OP_BOOTREQUEST &&
Joe Hershberger8f4b1352012-05-15 08:59:06 +000078 bp->bp_op != OP_BOOTREPLY &&
79 bp->bp_op != DHCP_OFFER &&
80 bp->bp_op != DHCP_ACK &&
81 bp->bp_op != DHCP_NAK)
wdenk3861aa52002-09-27 23:19:37 +000082 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +000083 else if (bp->bp_htype != HWT_ETHER)
84 retval = -4;
85 else if (bp->bp_hlen != HWL_ETHER)
86 retval = -5;
Joe Hershberger8f4b1352012-05-15 08:59:06 +000087 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
wdenk3861aa52002-09-27 23:19:37 +000088 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +000089
Robin Getz9e0a4d62009-07-23 03:01:03 -040090 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000091
92 return retval;
93}
94
95/*
96 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
97 */
Joe Hershberger8f4b1352012-05-15 08:59:06 +000098static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +000099{
Wilson Callan22bcd6e2007-07-28 10:56:13 -0400100#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger9d37a582012-05-23 07:59:18 +0000101 IPaddr_t tmp_ip;
102
wdenk4989f872004-03-14 15:06:13 +0000103 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
104 if (tmp_ip != 0)
105 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershberger1178f412012-05-23 07:58:06 +0000106 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
Wilson Callan22bcd6e2007-07-28 10:56:13 -0400107#endif
Joe Hershberger9d37a582012-05-23 07:59:18 +0000108 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
wdenk4989f872004-03-14 15:06:13 +0000109 if (strlen(bp->bp_file) > 0)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000110 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000111
Robin Getz9e0a4d62009-07-23 03:01:03 -0400112 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000113
114 /* Propagate to environment:
wdenk57b2d802003-06-27 21:31:46 +0000115 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000116 * not contain a new value
117 */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000118 if (*BootFile)
119 setenv("bootfile", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000120}
121
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000122static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000123{
124 if (curlen >= maxlen) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000125 printf("*** WARNING: %s is too long (%d - max: %d)"
126 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000127 curlen = maxlen - 1;
128 }
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000129 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000130}
131
Jon Loeliger54f35c22007-07-09 17:45:14 -0500132#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000133
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000134static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000135{
wdenk29e7f5a2004-03-12 00:14:09 +0000136 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000137
Robin Getz9e0a4d62009-07-23 03:01:03 -0400138 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000139 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000140
wdenk29e7f5a2004-03-12 00:14:09 +0000141 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000142
wdenk29e7f5a2004-03-12 00:14:09 +0000143 switch (*ext) {
144 /* Fixed length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000145 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000146 if (NetOurSubnetMask == 0)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000147 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000148 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000149 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000150 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000151 /* Variable length fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000152 case 3: /* Gateways list */
153 if (NetOurGatewayIP == 0)
154 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000155 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000156 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000157 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000158 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000159 break;
160 case 6:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000161 if (NetOurDNSIP == 0)
162 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500163#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000164 if ((NetOurDNS2IP == 0) && (size > 4))
165 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000166#endif
wdenk3861aa52002-09-27 23:19:37 +0000167 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000168 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000169 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000170 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000171 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000172 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000173 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000174 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000175 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000176 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000177 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000178 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000179 if (NetOurHostName[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000180 size = truncate_sz("Host Name",
181 sizeof(NetOurHostName), size);
182 memcpy(&NetOurHostName, ext + 2, size);
wdenk29e7f5a2004-03-12 00:14:09 +0000183 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000184 }
185 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000186 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000187 if (size == 2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000188 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000189 else if (size == 4)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000190 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000192 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000193 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000194 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000195 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000196 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000197 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000198 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000199 if (NetOurRootPath[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000200 size = truncate_sz("Root Path",
201 sizeof(NetOurRootPath), size);
202 memcpy(&NetOurRootPath, ext + 2, size);
wdenk29e7f5a2004-03-12 00:14:09 +0000203 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000204 }
205 break;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000206 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000207 /*
wdenk57b2d802003-06-27 21:31:46 +0000208 * This can be used to send the information of the
209 * vendor area in another file that the client can
210 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000211 */
212 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000213 /* IP host layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000214 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000215 if (NetOurNISDomain[0] == 0) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000216 size = truncate_sz("NIS Domain Name",
217 sizeof(NetOurNISDomain), size);
218 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk29e7f5a2004-03-12 00:14:09 +0000219 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000220 }
221 break;
Luuk Paulussen6380e012011-05-16 18:29:19 +0000222#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
223 case 42: /* NTP server IP */
224 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
225 break;
226#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000227 /* Application layer fields */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000228 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 /*
wdenk57b2d802003-06-27 21:31:46 +0000230 * Binary information to exchange specific
231 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000232 */
233 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000234 /* Reserved (custom) fields (128..254) */
235 }
wdenk3861aa52002-09-27 23:19:37 +0000236}
237
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000238static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000239{
wdenk29e7f5a2004-03-12 00:14:09 +0000240 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000241
Robin Getz9e0a4d62009-07-23 03:01:03 -0400242 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000243
wdenk29e7f5a2004-03-12 00:14:09 +0000244 while ((ext < end) && (*ext != 0xff)) {
245 if (*ext == 0) {
246 ext++;
247 } else {
248 u8 *opt = ext;
249
250 ext += ext[1] + 2;
251 if (ext <= end)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000252 BootpVendorFieldProcess(opt);
wdenk29e7f5a2004-03-12 00:14:09 +0000253 }
wdenk3861aa52002-09-27 23:19:37 +0000254 }
wdenk3861aa52002-09-27 23:19:37 +0000255
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000256 debug("[BOOTP] Received fields:\n");
Mike Frysingerd5695f42009-02-17 00:00:53 -0500257 if (NetOurSubnetMask)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400258 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000259
Mike Frysingerd5695f42009-02-17 00:00:53 -0500260 if (NetOurGatewayIP)
Robin Getz9e0a4d62009-07-23 03:01:03 -0400261 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000262
Robin Getz9e0a4d62009-07-23 03:01:03 -0400263 if (NetBootFileSize)
264 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000265
Robin Getz9e0a4d62009-07-23 03:01:03 -0400266 if (NetOurHostName[0])
267 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000268
Robin Getz9e0a4d62009-07-23 03:01:03 -0400269 if (NetOurRootPath[0])
270 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000271
Robin Getz9e0a4d62009-07-23 03:01:03 -0400272 if (NetOurNISDomain[0])
273 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000274
Robin Getz9e0a4d62009-07-23 03:01:03 -0400275 if (NetBootFileSize)
276 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen6380e012011-05-16 18:29:19 +0000277
278#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
279 if (NetNtpServerIP)
280 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
281#endif
wdenk3861aa52002-09-27 23:19:37 +0000282}
Simon Glassab068eb2011-06-13 16:13:12 -0700283
wdenk3861aa52002-09-27 23:19:37 +0000284/*
285 * Handle a BOOTP received packet.
286 */
287static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000288BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
289 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000290{
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000291 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000292
Robin Getz9e0a4d62009-07-23 03:01:03 -0400293 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000294 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000295
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000296 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000297
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000298 /* Filter out pkts we don't want */
299 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000300 return;
301
302 /*
wdenk29e7f5a2004-03-12 00:14:09 +0000303 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000304 */
305#ifdef CONFIG_STATUS_LED
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000306 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000307#endif
308
309 BootpCopyNetParams(bp); /* Store net parameters from reply */
310
311 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000312 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200313 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000314
315 NetSetTimeout(0, (thand_f *)0);
Simon Glass768cbf02011-12-10 11:08:06 +0000316 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000317
Robin Getz9e0a4d62009-07-23 03:01:03 -0400318 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000319
Simon Glass5234ad12011-10-27 06:24:32 +0000320 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000321}
Jon Loeligera9807e52007-07-10 11:05:02 -0500322#endif
wdenk3861aa52002-09-27 23:19:37 +0000323
324/*
325 * Timeout on BOOTP/DHCP request.
326 */
327static void
328BootpTimeout(void)
329{
330 if (BootpTry >= TIMEOUT_COUNT) {
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000331#ifdef CONFIG_BOOTP_MAY_FAIL
332 puts("\nRetry count exceeded\n");
benoit.thebaudeau@advans7232ee52012-07-19 01:23:21 +0000333 net_set_state(NETLOOP_FAIL);
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000334#else
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000335 puts("\nRetry count exceeded; starting again\n");
336 NetStartAgain();
Joe Hershberger8ca7fa02012-05-23 07:59:19 +0000337#endif
wdenk3861aa52002-09-27 23:19:37 +0000338 } else {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000339 NetSetTimeout(TIMEOUT, BootpTimeout);
340 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000341 }
342}
343
Ilya Yanok30876582012-09-17 10:26:25 +0000344#define put_vci(e, str) \
345 do { \
346 size_t vci_strlen = strlen(str); \
347 *e++ = 60; /* Vendor Class Identifier */ \
348 *e++ = vci_strlen; \
349 memcpy(e, str, vci_strlen); \
350 e += vci_strlen; \
351 } while (0)
352
wdenk3861aa52002-09-27 23:19:37 +0000353/*
354 * Initialize BOOTP extension fields in the request.
355 */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500356#if defined(CONFIG_CMD_DHCP)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000357static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
358 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000359{
wdenk29e7f5a2004-03-12 00:14:09 +0000360 u8 *start = e;
361 u8 *cnt;
Jason Hobbsea3202812011-08-31 05:37:31 +0000362#if defined(CONFIG_BOOTP_PXE)
363 char *uuid;
Jason Hobbsea3202812011-08-31 05:37:31 +0000364 u16 clientarch;
365#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000366
Jon Loeliger5336a762007-07-09 22:08:34 -0500367#if defined(CONFIG_BOOTP_VENDOREX)
wdenk29e7f5a2004-03-12 00:14:09 +0000368 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000369#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500370#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200371 char *hostname;
stroesee0aadfb2003-08-28 14:17:32 +0000372#endif
wdenk3861aa52002-09-27 23:19:37 +0000373
wdenk29e7f5a2004-03-12 00:14:09 +0000374 *e++ = 99; /* RFC1048 Magic Cookie */
375 *e++ = 130;
376 *e++ = 83;
377 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000378
wdenk29e7f5a2004-03-12 00:14:09 +0000379 *e++ = 53; /* DHCP Message Type */
380 *e++ = 1;
381 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000382
wdenk29e7f5a2004-03-12 00:14:09 +0000383 *e++ = 57; /* Maximum DHCP Message Size */
384 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000385 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
386 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000387
wdenk29e7f5a2004-03-12 00:14:09 +0000388 if (ServerID) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000389 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000390
wdenk29e7f5a2004-03-12 00:14:09 +0000391 *e++ = 54; /* ServerID */
392 *e++ = 4;
393 *e++ = tmp >> 24;
394 *e++ = tmp >> 16;
395 *e++ = tmp >> 8;
396 *e++ = tmp & 0xff;
397 }
wdenk3861aa52002-09-27 23:19:37 +0000398
wdenk29e7f5a2004-03-12 00:14:09 +0000399 if (RequestedIP) {
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000400 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000401
wdenk29e7f5a2004-03-12 00:14:09 +0000402 *e++ = 50; /* Requested IP */
403 *e++ = 4;
404 *e++ = tmp >> 24;
405 *e++ = tmp >> 16;
406 *e++ = tmp >> 8;
407 *e++ = tmp & 0xff;
408 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500409#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000410 hostname = getenv("hostname");
411 if (hostname) {
412 int hostnamelen = strlen(hostname);
wdenk29e7f5a2004-03-12 00:14:09 +0000413
414 *e++ = 12; /* Hostname */
415 *e++ = hostnamelen;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000416 memcpy(e, hostname, hostnamelen);
wdenk29e7f5a2004-03-12 00:14:09 +0000417 e += hostnamelen;
418 }
Jason Hobbsea3202812011-08-31 05:37:31 +0000419#endif
420
421#if defined(CONFIG_BOOTP_PXE)
422 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
423 *e++ = 93; /* Client System Architecture */
424 *e++ = 2;
425 *e++ = (clientarch >> 8) & 0xff;
426 *e++ = clientarch & 0xff;
427
428 *e++ = 94; /* Client Network Interface Identifier */
429 *e++ = 3;
430 *e++ = 1; /* type field for UNDI */
431 *e++ = 0; /* major revision */
432 *e++ = 0; /* minor revision */
433
434 uuid = getenv("pxeuuid");
435
436 if (uuid) {
437 if (uuid_str_valid(uuid)) {
438 *e++ = 97; /* Client Machine Identifier */
439 *e++ = 17;
440 *e++ = 0; /* type 0 - UUID */
441
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200442 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsea3202812011-08-31 05:37:31 +0000443 e += 16;
444 } else {
445 printf("Invalid pxeuuid: %s\n", uuid);
446 }
447 }
Ilya Yanok30876582012-09-17 10:26:25 +0000448#endif
Jason Hobbsea3202812011-08-31 05:37:31 +0000449
Ilya Yanok30876582012-09-17 10:26:25 +0000450#ifdef CONFIG_BOOTP_VCI_STRING
451 put_vci(e, CONFIG_BOOTP_VCI_STRING);
stroesee0aadfb2003-08-28 14:17:32 +0000452#endif
453
Jon Loeliger5336a762007-07-09 22:08:34 -0500454#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000455 x = dhcp_vendorex_prep(e);
456 if (x)
wdenk29e7f5a2004-03-12 00:14:09 +0000457 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000458#endif
459
wdenk29e7f5a2004-03-12 00:14:09 +0000460 *e++ = 55; /* Parameter Request List */
461 cnt = e++; /* Pointer to count of requested items */
462 *cnt = 0;
Jon Loeliger5336a762007-07-09 22:08:34 -0500463#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000464 *e++ = 1; /* Subnet Mask */
465 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000466#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000468 *e++ = 2;
469 *cnt += 1;
470#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500471#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000472 *e++ = 3; /* Router Option */
473 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500475#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000476 *e++ = 6; /* DNS Server(s) */
477 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000478#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500479#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000480 *e++ = 12; /* Hostname */
481 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000482#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500483#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000484 *e++ = 13; /* Boot File Size */
485 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000486#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500487#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000488 *e++ = 17; /* Boot path */
489 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000490#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500491#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000492 *e++ = 40; /* NIS Domain name request */
493 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000494#endif
Jon Loeliger5336a762007-07-09 22:08:34 -0500495#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000496 *e++ = 42;
497 *cnt += 1;
498#endif
Jason Liu1c7dca52010-11-14 12:23:09 +0800499 /* no options, so back up to avoid sending an empty request list */
500 if (*cnt == 0)
501 e -= 2;
502
wdenk29e7f5a2004-03-12 00:14:09 +0000503 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000504
wdenk29e7f5a2004-03-12 00:14:09 +0000505 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000506#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glassa0978eb2011-02-02 15:03:28 -0800507 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk29e7f5a2004-03-12 00:14:09 +0000508 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000509#endif
510
wdenk29e7f5a2004-03-12 00:14:09 +0000511 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000512}
513
Jon Loeligera9807e52007-07-10 11:05:02 -0500514#else
wdenk3861aa52002-09-27 23:19:37 +0000515/*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000516 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000517 */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000518static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000519{
wdenk29e7f5a2004-03-12 00:14:09 +0000520 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000521
wdenk29e7f5a2004-03-12 00:14:09 +0000522 *e++ = 99; /* RFC1048 Magic Cookie */
523 *e++ = 130;
524 *e++ = 83;
525 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000526
Jon Loeliger54f35c22007-07-09 17:45:14 -0500527#if defined(CONFIG_CMD_DHCP)
wdenk29e7f5a2004-03-12 00:14:09 +0000528 *e++ = 53; /* DHCP Message Type */
529 *e++ = 1;
530 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000531
wdenk29e7f5a2004-03-12 00:14:09 +0000532 *e++ = 57; /* Maximum DHCP Message Size */
533 *e++ = 2;
Joe Hershbergerceba4472012-05-23 07:58:14 +0000534 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
535 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeligera9807e52007-07-10 11:05:02 -0500536#endif
wdenk3861aa52002-09-27 23:19:37 +0000537
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000538#if defined(CONFIG_BOOTP_VCI_STRING) || \
539 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
540#ifdef CONFIG_SPL_BUILD
541 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
542#else
Ilya Yanok30876582012-09-17 10:26:25 +0000543 put_vci(e, CONFIG_BOOTP_VCI_STRING);
544#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000545#endif
Ilya Yanok30876582012-09-17 10:26:25 +0000546
Jon Loeliger5336a762007-07-09 22:08:34 -0500547#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk29e7f5a2004-03-12 00:14:09 +0000548 *e++ = 1; /* Subnet mask request */
549 *e++ = 4;
550 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000551#endif
552
Jon Loeliger5336a762007-07-09 22:08:34 -0500553#if defined(CONFIG_BOOTP_GATEWAY)
wdenk29e7f5a2004-03-12 00:14:09 +0000554 *e++ = 3; /* Default gateway request */
555 *e++ = 4;
556 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000557#endif
558
Jon Loeliger5336a762007-07-09 22:08:34 -0500559#if defined(CONFIG_BOOTP_DNS)
wdenk29e7f5a2004-03-12 00:14:09 +0000560 *e++ = 6; /* Domain Name Server */
561 *e++ = 4;
562 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000563#endif
564
Jon Loeliger5336a762007-07-09 22:08:34 -0500565#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk29e7f5a2004-03-12 00:14:09 +0000566 *e++ = 12; /* Host name request */
567 *e++ = 32;
568 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000569#endif
570
Jon Loeliger5336a762007-07-09 22:08:34 -0500571#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk29e7f5a2004-03-12 00:14:09 +0000572 *e++ = 13; /* Boot file size */
573 *e++ = 2;
574 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000575#endif
576
Jon Loeliger5336a762007-07-09 22:08:34 -0500577#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk29e7f5a2004-03-12 00:14:09 +0000578 *e++ = 17; /* Boot path */
579 *e++ = 32;
580 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000581#endif
582
Jon Loeliger5336a762007-07-09 22:08:34 -0500583#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk29e7f5a2004-03-12 00:14:09 +0000584 *e++ = 40; /* NIS Domain name request */
585 *e++ = 32;
586 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000587#endif
Luuk Paulussen6380e012011-05-16 18:29:19 +0000588#if defined(CONFIG_BOOTP_NTPSERVER)
589 *e++ = 42;
590 *e++ = 4;
591 e += 4;
592#endif
wdenk3861aa52002-09-27 23:19:37 +0000593
wdenk29e7f5a2004-03-12 00:14:09 +0000594 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000595
wdenk29e7f5a2004-03-12 00:14:09 +0000596 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000597}
Jon Loeligera9807e52007-07-10 11:05:02 -0500598#endif
wdenk3861aa52002-09-27 23:19:37 +0000599
600void
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000601BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000602{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000603 uchar *pkt, *iphdr;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000604 struct Bootp_t *bp;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000605 int extlen, pktlen, iplen;
606 int eth_hdr_size;
Joe Hershberger797f2c52012-05-23 07:57:58 +0000607#ifdef CONFIG_BOOTP_RANDOM_DELAY
608 ulong i, rand_ms;
609#endif
wdenk3861aa52002-09-27 23:19:37 +0000610
Simon Glass768cbf02011-12-10 11:08:06 +0000611 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger54f35c22007-07-09 17:45:14 -0500612#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000613 dhcp_state = INIT;
614#endif
615
616#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger797f2c52012-05-23 07:57:58 +0000617 if (BootpTry == 0)
618 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000619
Joe Hershberger797f2c52012-05-23 07:57:58 +0000620 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
621 rand_ms = rand() >> (22 - BootpTry);
622 else /* After 3rd BOOTP request max 8192 * 1ms */
623 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000624
Joe Hershberger797f2c52012-05-23 07:57:58 +0000625 printf("Random delay: %ld ms...\n", rand_ms);
626 for (i = 0; i < rand_ms; i++)
wdenk3861aa52002-09-27 23:19:37 +0000627 udelay(1000); /*Wait 1ms*/
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000628
wdenk3861aa52002-09-27 23:19:37 +0000629#endif /* CONFIG_BOOTP_RANDOM_DELAY */
630
631 printf("BOOTP broadcast %d\n", ++BootpTry);
632 pkt = NetTxPacket;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000633 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000634
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000635 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
636 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000637
638 /*
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000639 * Next line results in incorrect packet size being transmitted,
640 * resulting in errors in some DHCP servers, reporting missing bytes.
641 * Size must be set in packet header after extension length has been
642 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000643 * C. Hallinan, DS4.COM, Inc.
644 */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000645 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000646 sizeof (struct Bootp_t)); */
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000647 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000648 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000649
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000650 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000651 bp->bp_op = OP_BOOTREQUEST;
652 bp->bp_htype = HWT_ETHER;
653 bp->bp_hlen = HWL_ETHER;
654 bp->bp_hops = 0;
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200655 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000656 NetWriteIP(&bp->bp_ciaddr, 0);
657 NetWriteIP(&bp->bp_yiaddr, 0);
658 NetWriteIP(&bp->bp_siaddr, 0);
659 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000660 memcpy(bp->bp_chaddr, NetOurEther, 6);
661 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000662
663 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger54f35c22007-07-09 17:45:14 -0500664#if defined(CONFIG_CMD_DHCP)
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000665 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000666#else
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000667 extlen = BootpExtended((u8 *)bp->bp_vend);
Jon Loeligera9807e52007-07-10 11:05:02 -0500668#endif
wdenk3861aa52002-09-27 23:19:37 +0000669
670 /*
671 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200672 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000673 */
674 BootpID = ((ulong)NetOurEther[2] << 24)
675 | ((ulong)NetOurEther[3] << 16)
676 | ((ulong)NetOurEther[4] << 8)
677 | (ulong)NetOurEther[5];
678 BootpID += get_timer(0);
wdenk29e7f5a2004-03-12 00:14:09 +0000679 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000680 NetCopyLong(&bp->bp_id, &BootpID);
681
682 /*
683 * Calculate proper packet lengths taking into account the
684 * variable size of the options field
685 */
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000686 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
687 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000688 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200689 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000690
Jon Loeliger54f35c22007-07-09 17:45:14 -0500691#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000692 dhcp_state = SELECTING;
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000693 net_set_udp_handler(DhcpHandler);
wdenk3861aa52002-09-27 23:19:37 +0000694#else
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000695 net_set_udp_handler(BootpHandler);
Jon Loeligera9807e52007-07-10 11:05:02 -0500696#endif
wdenk3861aa52002-09-27 23:19:37 +0000697 NetSendPacket(NetTxPacket, pktlen);
698}
699
Jon Loeliger54f35c22007-07-09 17:45:14 -0500700#if defined(CONFIG_CMD_DHCP)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000701static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000702{
wdenkb02744a2003-04-05 00:53:31 +0000703 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000704 int oplen, size;
Wolfgang Denk2b184222009-09-11 09:05:32 +0200705#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
706 int *to_ptr;
707#endif
wdenk3861aa52002-09-27 23:19:37 +0000708
wdenk29e7f5a2004-03-12 00:14:09 +0000709 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000710 oplen = *(popt + 1);
wdenk29e7f5a2004-03-12 00:14:09 +0000711 switch (*popt) {
712 case 1:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000713 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000714 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500715#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000716 case 2: /* Time offset */
Wolfgang Denk2b184222009-09-11 09:05:32 +0200717 to_ptr = &NetTimeOffset;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000718 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
719 NetTimeOffset = ntohl(NetTimeOffset);
wdenkb4ad9622005-04-01 00:25:43 +0000720 break;
721#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000722 case 3:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000723 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000724 break;
725 case 6:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000726 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger5336a762007-07-09 22:08:34 -0500727#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000728 if (*(popt + 1) > 4)
729 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesee0aadfb2003-08-28 14:17:32 +0000730#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000731 break;
732 case 12:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000733 size = truncate_sz("Host Name",
734 sizeof(NetOurHostName), oplen);
735 memcpy(&NetOurHostName, popt + 2, size);
wdenk29e7f5a2004-03-12 00:14:09 +0000736 NetOurHostName[size] = 0;
737 break;
738 case 15: /* Ignore Domain Name Option */
739 break;
740 case 17:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000741 size = truncate_sz("Root Path",
742 sizeof(NetOurRootPath), oplen);
743 memcpy(&NetOurRootPath, popt + 2, size);
wdenk29e7f5a2004-03-12 00:14:09 +0000744 NetOurRootPath[size] = 0;
745 break;
Brian Rzycki5a672a22012-09-11 09:22:53 +0000746 case 28: /* Ignore Broadcast Address Option */
747 break;
Jon Loeliger5336a762007-07-09 22:08:34 -0500748#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000749 case 42: /* NTP server IP */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000750 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkb4ad9622005-04-01 00:25:43 +0000751 break;
752#endif
wdenk29e7f5a2004-03-12 00:14:09 +0000753 case 51:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000754 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000755 break;
756 case 53: /* Ignore Message Type Option */
757 break;
758 case 54:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000759 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk29e7f5a2004-03-12 00:14:09 +0000760 break;
761 case 58: /* Ignore Renewal Time Option */
762 break;
763 case 59: /* Ignore Rebinding Time Option */
764 break;
Wolfgang Denk012429c2006-03-12 18:26:46 +0100765 case 66: /* Ignore TFTP server name */
766 break;
767 case 67: /* vendor opt bootfile */
768 /*
769 * I can't use dhcp_vendorex_proc here because I need
770 * to write into the bootp packet - even then I had to
771 * pass the bootp packet pointer into here as the
772 * second arg
773 */
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000774 size = truncate_sz("Opt Boot File",
Wolfgang Denk012429c2006-03-12 18:26:46 +0100775 sizeof(bp->bp_file),
776 oplen);
777 if (bp->bp_file[0] == '\0' && size > 0) {
778 /*
779 * only use vendor boot file if we didn't
780 * receive a boot file in the main non-vendor
781 * part of the packet - god only knows why
782 * some vendors chose not to use this perfectly
783 * good spot to store the boot file (join on
784 * Tru64 Unix) it seems mind bogglingly crazy
785 * to me
786 */
787 printf("*** WARNING: using vendor "
788 "optional boot file\n");
789 memcpy(bp->bp_file, popt + 2, size);
790 bp->bp_file[size] = '\0';
791 }
792 break;
wdenk29e7f5a2004-03-12 00:14:09 +0000793 default:
Jon Loeliger5336a762007-07-09 22:08:34 -0500794#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000795 if (dhcp_vendorex_proc(popt))
wdenk57b2d802003-06-27 21:31:46 +0000796 break;
wdenk3861aa52002-09-27 23:19:37 +0000797#endif
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000798 printf("*** Unhandled DHCP Option in OFFER/ACK:"
799 " %d\n", *popt);
wdenk29e7f5a2004-03-12 00:14:09 +0000800 break;
wdenk3861aa52002-09-27 23:19:37 +0000801 }
802 popt += oplen + 2; /* Process next option */
803 }
804}
805
806static int DhcpMessageType(unsigned char *popt)
807{
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000808 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000809 return -1;
810
811 popt += 4;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000812 while (*popt != 0xff) {
813 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000814 return *(popt + 2);
815 popt += *(popt + 1) + 2; /* Scan through all options */
816 }
817 return -1;
818}
819
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000820static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000821{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000822 uchar *pkt, *iphdr;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000823 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000824 int pktlen, iplen, extlen;
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000825 int eth_hdr_size;
wdenkcc1e2562003-03-06 13:39:27 +0000826 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000827
Robin Getz9e0a4d62009-07-23 03:01:03 -0400828 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000829 pkt = NetTxPacket;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000830 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000831
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000832 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
833 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000834
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000835 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000836 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000837
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000838 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000839 bp->bp_op = OP_BOOTREQUEST;
840 bp->bp_htype = HWT_ETHER;
841 bp->bp_hlen = HWL_ETHER;
842 bp->bp_hops = 0;
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200843 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000844 /* Do not set the client IP, your IP, or server IP yet, since it
845 * hasn't been ACK'ed by the server yet */
Justin Flammia01f256b2007-10-29 17:40:35 -0400846
Wolfgang Denk8a933fb2006-10-12 00:01:08 +0200847 /*
Wolfgang Denk68e83a82006-10-09 01:26:14 +0200848 * RFC3046 requires Relay Agents to discard packets with
849 * nonzero and offered giaddr
850 */
851 NetWriteIP(&bp->bp_giaddr, 0);
852
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000853 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000854
855 /*
856 * ID is the id of the OFFER packet
857 */
858
859 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
860
861 /*
862 * Copy options from OFFER packet if present
863 */
Justin Flammia01f256b2007-10-29 17:40:35 -0400864
865 /* Copy offered IP into the parameters request list */
866 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000867 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
868 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000869
Joe Hershbergerdb3e6e42012-05-23 07:59:10 +0000870 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
871 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger2ed5b492012-05-23 07:59:07 +0000872 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000873
Aras Vaichas72aa3f32008-03-26 09:43:57 +1100874#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
875 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
876#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerb1e94762012-05-23 07:59:11 +0000877 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000878 NetSendPacket(NetTxPacket, pktlen);
879}
880
881/*
882 * Handle DHCP received packets.
883 */
884static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000885DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
886 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000887{
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000888 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000889
Robin Getz9e0a4d62009-07-23 03:01:03 -0400890 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000891 src, dest, len, dhcp_state);
892
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000893 /* Filter out pkts we don't want */
894 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000895 return;
896
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000897 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
898 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000899
900 switch (dhcp_state) {
901 case SELECTING:
902 /*
903 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000904 * to arrive. Then select one, and generate DHCPREQUEST
905 * response. If filename is in format we recognize, assume it
906 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000907 */
Robin Getz9e0a4d62009-07-23 03:01:03 -0400908 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200909#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000910 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200911 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000912 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200913#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000914
Robin Getz9e0a4d62009-07-23 03:01:03 -0400915 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000916 dhcp_state = REQUESTING;
stroese5fa6e902003-04-10 13:26:44 +0000917
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000918 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
919 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk012429c2006-03-12 18:26:46 +0100920 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000921
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200922 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000923 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200924#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000925 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200926#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000927
928 return;
929 break;
930 case REQUESTING:
Robin Getz9e0a4d62009-07-23 03:01:03 -0400931 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000932
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000933 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
934 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
935 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk012429c2006-03-12 18:26:46 +0100936 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000937 /* Store net params from reply */
938 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000939 dhcp_state = BOUND;
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000940 printf("DHCP client bound to address %pI4\n",
941 &NetOurIP);
Simon Glass768cbf02011-12-10 11:08:06 +0000942 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000943 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000944
Simon Glass5234ad12011-10-27 06:24:32 +0000945 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000946 return;
947 }
948 break;
Remy Bohmer03a44492008-08-20 11:30:28 +0200949 case BOUND:
950 /* DHCP client bound to address */
951 break;
wdenk3861aa52002-09-27 23:19:37 +0000952 default:
Joe Hershberger8f4b1352012-05-15 08:59:06 +0000953 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000954 break;
955 }
956
957}
958
959void DhcpRequest(void)
960{
961 BootpRequest();
962}
Wolfgang Denkf7a7f082007-11-03 23:09:27 +0100963#endif /* CONFIG_CMD_DHCP */