blob: fdb97cb56252521237baf7b2f02c94f42724cd8d [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
Kim Phillipsdb7720b2012-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 Hershberger3090b7e32012-05-15 08:59:06 +000024#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000025
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020026#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000027#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000028# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000029#else
wdenk232c1502004-03-12 00:14:09 +000030# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000031#endif
32
Joe Hershberger3090b7e32012-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 Hershberger3090b7e32012-05-15 08:59:06 +000036#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-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 Loeliger643d1ab2007-07-09 17:45:14 -050043#if defined(CONFIG_CMD_DHCP)
Kim Phillips06370592012-10-29 13:34:33 +000044static dhcp_state_t dhcp_state = INIT;
45static unsigned long dhcp_leasetime;
46static IPaddr_t NetDHCPServerIP;
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
Jon Loeliger610f2e92007-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 Hershberger3090b7e32012-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 Hershbergerf8315732012-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 Hershberger3090b7e32012-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 Hershberger3090b7e32012-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 Getz0ebf04c2009-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 Hershberger3090b7e32012-05-15 08:59:06 +000098static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +000099{
Wilson Callan5d110f02007-07-28 10:56:13 -0400100#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000101 IPaddr_t tmp_ip;
102
wdenk3d3befa2004-03-14 15:06:13 +0000103 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
104 if (tmp_ip != 0)
105 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershbergercb487f52012-05-23 07:58:06 +0000106 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400107#endif
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000108 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
wdenk3d3befa2004-03-14 15:06:13 +0000109 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000110 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000111
Robin Getz0ebf04c2009-07-23 03:01:03 -0400112 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000113
114 /* Propagate to environment:
wdenk8bde7f72003-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 Hershberger3090b7e32012-05-15 08:59:06 +0000118 if (*BootFile)
119 setenv("bootfile", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000120}
121
Joe Hershberger3090b7e32012-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 Hershberger3090b7e32012-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 Hershberger3090b7e32012-05-15 08:59:06 +0000129 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000130}
131
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500132#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000133
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000134static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000135{
wdenk232c1502004-03-12 00:14:09 +0000136 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000137
Robin Getz0ebf04c2009-07-23 03:01:03 -0400138 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000139 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000140
wdenk232c1502004-03-12 00:14:09 +0000141 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000142
wdenk232c1502004-03-12 00:14:09 +0000143 switch (*ext) {
144 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000145 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000146 if (NetOurSubnetMask == 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000147 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000148 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000149 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000150 break;
wdenk232c1502004-03-12 00:14:09 +0000151 /* Variable length fields */
Joe Hershberger3090b7e32012-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 Hershberger3090b7e32012-05-15 08:59:06 +0000156 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000157 break;
Joe Hershberger3090b7e32012-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 Hershberger3090b7e32012-05-15 08:59:06 +0000161 if (NetOurDNSIP == 0)
162 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500163#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000164 if ((NetOurDNS2IP == 0) && (size > 4))
165 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000166#endif
wdenk3861aa52002-09-27 23:19:37 +0000167 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000168 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000169 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000170 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000171 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000172 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000173 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000174 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000175 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000176 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000177 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000178 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000179 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000180 size = truncate_sz("Host Name",
181 sizeof(NetOurHostName), size);
182 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000183 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000184 }
185 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000186 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000187 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000188 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000189 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000190 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000192 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000193 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000194 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000195 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000196 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000197 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000198 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000199 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000200 size = truncate_sz("Root Path",
201 sizeof(NetOurRootPath), size);
202 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000203 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000204 }
205 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000206 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000207 /*
wdenk8bde7f72003-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;
wdenk232c1502004-03-12 00:14:09 +0000213 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000214 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000215 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000216 size = truncate_sz("NIS Domain Name",
217 sizeof(NetOurNISDomain), size);
218 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000219 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000220 }
221 break;
Luuk Paulussen09e3a672011-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
wdenk232c1502004-03-12 00:14:09 +0000227 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000228 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 /*
wdenk8bde7f72003-06-27 21:31:46 +0000230 * Binary information to exchange specific
231 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000232 */
233 break;
wdenk232c1502004-03-12 00:14:09 +0000234 /* Reserved (custom) fields (128..254) */
235 }
wdenk3861aa52002-09-27 23:19:37 +0000236}
237
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000238static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000239{
wdenk232c1502004-03-12 00:14:09 +0000240 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000241
Robin Getz0ebf04c2009-07-23 03:01:03 -0400242 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000243
wdenk232c1502004-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 Hershberger3090b7e32012-05-15 08:59:06 +0000252 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000253 }
wdenk3861aa52002-09-27 23:19:37 +0000254 }
wdenk3861aa52002-09-27 23:19:37 +0000255
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000256 debug("[BOOTP] Received fields:\n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500257 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400258 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000259
Mike Frysingerb6446b62009-02-17 00:00:53 -0500260 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400261 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000262
Robin Getz0ebf04c2009-07-23 03:01:03 -0400263 if (NetBootFileSize)
264 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000265
Robin Getz0ebf04c2009-07-23 03:01:03 -0400266 if (NetOurHostName[0])
267 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000268
Robin Getz0ebf04c2009-07-23 03:01:03 -0400269 if (NetOurRootPath[0])
270 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000271
Robin Getz0ebf04c2009-07-23 03:01:03 -0400272 if (NetOurNISDomain[0])
273 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000274
Robin Getz0ebf04c2009-07-23 03:01:03 -0400275 if (NetBootFileSize)
276 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-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 Glass09349862011-06-13 16:13:12 -0700283
wdenk3861aa52002-09-27 23:19:37 +0000284/*
285 * Handle a BOOTP received packet.
286 */
287static void
Luca Ceresoli03eb1292011-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 Hershberger3090b7e32012-05-15 08:59:06 +0000291 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000292
Robin Getz0ebf04c2009-07-23 03:01:03 -0400293 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000294 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000295
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000296 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000297
Joe Hershberger3090b7e32012-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 /*
wdenk232c1502004-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 Hershberger3090b7e32012-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 Hershberger3090b7e32012-05-15 08:59:06 +0000312 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-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 Glass573f14f2011-12-10 11:08:06 +0000316 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000317
Robin Getz0ebf04c2009-07-23 03:01:03 -0400318 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000319
Simon Glasse4a3d572011-10-27 06:24:32 +0000320 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000321}
Jon Loeliger610f2e92007-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 Hershberger2c00e092012-05-23 07:59:19 +0000331#ifdef CONFIG_BOOTP_MAY_FAIL
332 puts("\nRetry count exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000333 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000334#else
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000335 puts("\nRetry count exceeded; starting again\n");
336 NetStartAgain();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000337#endif
wdenk3861aa52002-09-27 23:19:37 +0000338 } else {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000339 NetSetTimeout(TIMEOUT, BootpTimeout);
340 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000341 }
342}
343
Ilya Yanok9ace17c2012-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 Loeliger643d1ab2007-07-09 17:45:14 -0500356#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-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{
wdenk232c1502004-03-12 00:14:09 +0000360 u8 *start = e;
361 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000362#if defined(CONFIG_BOOTP_PXE)
363 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000364 u16 clientarch;
365#endif
wdenk232c1502004-03-12 00:14:09 +0000366
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500367#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000368 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000369#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500370#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200371 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000372#endif
wdenk3861aa52002-09-27 23:19:37 +0000373
wdenk232c1502004-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
wdenk232c1502004-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
wdenk232c1502004-03-12 00:14:09 +0000383 *e++ = 57; /* Maximum DHCP Message Size */
384 *e++ = 2;
Joe Hershbergerf8315732012-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
wdenk232c1502004-03-12 00:14:09 +0000388 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000389 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000390
wdenk232c1502004-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
wdenk232c1502004-03-12 00:14:09 +0000399 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000400 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000401
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500409#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000410 hostname = getenv("hostname");
411 if (hostname) {
412 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000413
414 *e++ = 12; /* Hostname */
415 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000416 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000417 e += hostnamelen;
418 }
stroesefe389a82003-08-28 14:17:32 +0000419#endif
420
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000421#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 Marczakd718ded2014-04-02 10:20:03 +0200442 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000443 e += 16;
444 } else {
445 printf("Invalid pxeuuid: %s\n", uuid);
446 }
447 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000448#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000449
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000450#ifdef CONFIG_BOOTP_VCI_STRING
451 put_vci(e, CONFIG_BOOTP_VCI_STRING);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000452#endif
453
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500454#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000455 x = dhcp_vendorex_prep(e);
456 if (x)
wdenk232c1502004-03-12 00:14:09 +0000457 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000458#endif
459
wdenk232c1502004-03-12 00:14:09 +0000460 *e++ = 55; /* Parameter Request List */
461 cnt = e++; /* Pointer to count of requested items */
462 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500463#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 1; /* Subnet Mask */
465 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000466#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000468 *e++ = 2;
469 *cnt += 1;
470#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500471#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 3; /* Router Option */
473 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500475#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000476 *e++ = 6; /* DNS Server(s) */
477 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000478#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500479#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000480 *e++ = 12; /* Hostname */
481 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000482#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500483#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000484 *e++ = 13; /* Boot File Size */
485 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000486#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500487#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000488 *e++ = 17; /* Boot path */
489 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000490#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500491#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000492 *e++ = 40; /* NIS Domain name request */
493 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000494#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500495#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000496 *e++ = 42;
497 *cnt += 1;
498#endif
Jason Liu258ccd62010-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
wdenk232c1502004-03-12 00:14:09 +0000503 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000504
wdenk232c1502004-03-12 00:14:09 +0000505 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000506#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800507 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000508 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000509#endif
510
wdenk232c1502004-03-12 00:14:09 +0000511 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000512}
513
Jon Loeliger610f2e92007-07-10 11:05:02 -0500514#else
wdenk3861aa52002-09-27 23:19:37 +0000515/*
Joe Hershberger3090b7e32012-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 Hershberger3090b7e32012-05-15 08:59:06 +0000518static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000519{
wdenk232c1502004-03-12 00:14:09 +0000520 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000521
wdenk232c1502004-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 Loeliger643d1ab2007-07-09 17:45:14 -0500527#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-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
wdenk232c1502004-03-12 00:14:09 +0000532 *e++ = 57; /* Maximum DHCP Message Size */
533 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000534 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
535 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500536#endif
wdenk3861aa52002-09-27 23:19:37 +0000537
Ilya Yanok7ac2fe22012-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 Yanok9ace17c2012-09-17 10:26:25 +0000543 put_vci(e, CONFIG_BOOTP_VCI_STRING);
544#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000545#endif
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000546
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500547#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500553#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500559#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500565#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500571#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500577#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-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 Loeliger1fe80d72007-07-09 22:08:34 -0500583#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-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 Paulussen09e3a672011-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
wdenk232c1502004-03-12 00:14:09 +0000594 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000595
wdenk232c1502004-03-12 00:14:09 +0000596 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000597}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500598#endif
wdenk3861aa52002-09-27 23:19:37 +0000599
600void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000601BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000602{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000603 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000604 struct Bootp_t *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000605 int extlen, pktlen, iplen;
606 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000607#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200608 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000609#endif
wdenk3861aa52002-09-27 23:19:37 +0000610
Simon Glass573f14f2011-12-10 11:08:06 +0000611 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-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 Hershbergereafc8db2012-05-23 07:57:58 +0000617 if (BootpTry == 0)
618 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000619
Joe Hershbergereafc8db2012-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 Hershbergereafc8db2012-05-23 07:57:58 +0000625 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200626 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000627
wdenk3861aa52002-09-27 23:19:37 +0000628#endif /* CONFIG_BOOTP_RANDOM_DELAY */
629
630 printf("BOOTP broadcast %d\n", ++BootpTry);
631 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000632 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000633
Joe Hershbergerae446f52012-05-23 07:59:10 +0000634 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
635 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000636
637 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000638 * Next line results in incorrect packet size being transmitted,
639 * resulting in errors in some DHCP servers, reporting missing bytes.
640 * Size must be set in packet header after extension length has been
641 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000642 * C. Hallinan, DS4.COM, Inc.
643 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000644 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000645 sizeof (struct Bootp_t)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000646 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000647 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000648
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000649 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000650 bp->bp_op = OP_BOOTREQUEST;
651 bp->bp_htype = HWT_ETHER;
652 bp->bp_hlen = HWL_ETHER;
653 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200654 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000655 NetWriteIP(&bp->bp_ciaddr, 0);
656 NetWriteIP(&bp->bp_yiaddr, 0);
657 NetWriteIP(&bp->bp_siaddr, 0);
658 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000659 memcpy(bp->bp_chaddr, NetOurEther, 6);
660 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000661
662 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500663#if defined(CONFIG_CMD_DHCP)
Joe Hershbergerae446f52012-05-23 07:59:10 +0000664 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000665#else
Joe Hershbergerae446f52012-05-23 07:59:10 +0000666 extlen = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500667#endif
wdenk3861aa52002-09-27 23:19:37 +0000668
669 /*
670 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200671 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000672 */
673 BootpID = ((ulong)NetOurEther[2] << 24)
674 | ((ulong)NetOurEther[3] << 16)
675 | ((ulong)NetOurEther[4] << 8)
676 | (ulong)NetOurEther[5];
677 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000678 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000679 NetCopyLong(&bp->bp_id, &BootpID);
680
681 /*
682 * Calculate proper packet lengths taking into account the
683 * variable size of the options field
684 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000685 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
686 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000687 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200688 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000689
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500690#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000691 dhcp_state = SELECTING;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000692 net_set_udp_handler(DhcpHandler);
wdenk3861aa52002-09-27 23:19:37 +0000693#else
Joe Hershbergerece223b2012-05-23 07:59:15 +0000694 net_set_udp_handler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500695#endif
wdenk3861aa52002-09-27 23:19:37 +0000696 NetSendPacket(NetTxPacket, pktlen);
697}
698
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500699#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000700static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000701{
wdenk3e386912003-04-05 00:53:31 +0000702 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000703 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200704#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
705 int *to_ptr;
706#endif
wdenk3861aa52002-09-27 23:19:37 +0000707
wdenk232c1502004-03-12 00:14:09 +0000708 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000709 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000710 switch (*popt) {
711 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000712 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000713 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500714#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000715 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200716 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000717 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
718 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000719 break;
720#endif
wdenk232c1502004-03-12 00:14:09 +0000721 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000722 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000723 break;
724 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000725 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500726#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000727 if (*(popt + 1) > 4)
728 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000729#endif
wdenk232c1502004-03-12 00:14:09 +0000730 break;
731 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000732 size = truncate_sz("Host Name",
733 sizeof(NetOurHostName), oplen);
734 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000735 NetOurHostName[size] = 0;
736 break;
737 case 15: /* Ignore Domain Name Option */
738 break;
739 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000740 size = truncate_sz("Root Path",
741 sizeof(NetOurRootPath), oplen);
742 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000743 NetOurRootPath[size] = 0;
744 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000745 case 28: /* Ignore Broadcast Address Option */
746 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500747#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000748 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000749 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000750 break;
751#endif
wdenk232c1502004-03-12 00:14:09 +0000752 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000753 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000754 break;
755 case 53: /* Ignore Message Type Option */
756 break;
757 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000758 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000759 break;
760 case 58: /* Ignore Renewal Time Option */
761 break;
762 case 59: /* Ignore Rebinding Time Option */
763 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100764 case 66: /* Ignore TFTP server name */
765 break;
766 case 67: /* vendor opt bootfile */
767 /*
768 * I can't use dhcp_vendorex_proc here because I need
769 * to write into the bootp packet - even then I had to
770 * pass the bootp packet pointer into here as the
771 * second arg
772 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000773 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100774 sizeof(bp->bp_file),
775 oplen);
776 if (bp->bp_file[0] == '\0' && size > 0) {
777 /*
778 * only use vendor boot file if we didn't
779 * receive a boot file in the main non-vendor
780 * part of the packet - god only knows why
781 * some vendors chose not to use this perfectly
782 * good spot to store the boot file (join on
783 * Tru64 Unix) it seems mind bogglingly crazy
784 * to me
785 */
786 printf("*** WARNING: using vendor "
787 "optional boot file\n");
788 memcpy(bp->bp_file, popt + 2, size);
789 bp->bp_file[size] = '\0';
790 }
791 break;
wdenk232c1502004-03-12 00:14:09 +0000792 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500793#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000794 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000795 break;
wdenk3861aa52002-09-27 23:19:37 +0000796#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000797 printf("*** Unhandled DHCP Option in OFFER/ACK:"
798 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000799 break;
wdenk3861aa52002-09-27 23:19:37 +0000800 }
801 popt += oplen + 2; /* Process next option */
802 }
803}
804
805static int DhcpMessageType(unsigned char *popt)
806{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000807 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000808 return -1;
809
810 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000811 while (*popt != 0xff) {
812 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000813 return *(popt + 2);
814 popt += *(popt + 1) + 2; /* Scan through all options */
815 }
816 return -1;
817}
818
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000819static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000820{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000821 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000822 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000823 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000824 int eth_hdr_size;
wdenk47cd00f2003-03-06 13:39:27 +0000825 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000826
Robin Getz0ebf04c2009-07-23 03:01:03 -0400827 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000828 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000829 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000830
Joe Hershbergerae446f52012-05-23 07:59:10 +0000831 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
832 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000833
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000834 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000835 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000836
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000837 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000838 bp->bp_op = OP_BOOTREQUEST;
839 bp->bp_htype = HWT_ETHER;
840 bp->bp_hlen = HWL_ETHER;
841 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200842 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000843 /* Do not set the client IP, your IP, or server IP yet, since it
844 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400845
Wolfgang Denkc6686702006-10-12 00:01:08 +0200846 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200847 * RFC3046 requires Relay Agents to discard packets with
848 * nonzero and offered giaddr
849 */
850 NetWriteIP(&bp->bp_giaddr, 0);
851
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000852 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000853
854 /*
855 * ID is the id of the OFFER packet
856 */
857
858 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
859
860 /*
861 * Copy options from OFFER packet if present
862 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400863
864 /* Copy offered IP into the parameters request list */
865 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000866 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
867 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000868
Joe Hershbergerae446f52012-05-23 07:59:10 +0000869 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
870 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000871 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000872
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100873#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
874 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
875#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000876 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000877 NetSendPacket(NetTxPacket, pktlen);
878}
879
880/*
881 * Handle DHCP received packets.
882 */
883static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000884DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
885 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000886{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000887 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000888
Robin Getz0ebf04c2009-07-23 03:01:03 -0400889 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000890 src, dest, len, dhcp_state);
891
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000892 /* Filter out pkts we don't want */
893 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000894 return;
895
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000896 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
897 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000898
899 switch (dhcp_state) {
900 case SELECTING:
901 /*
902 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000903 * to arrive. Then select one, and generate DHCPREQUEST
904 * response. If filename is in format we recognize, assume it
905 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000906 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400907 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200908#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000909 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200910 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000911 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200912#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000913
Robin Getz0ebf04c2009-07-23 03:01:03 -0400914 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000915 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000916
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000917 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
918 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100919 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000920
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200921 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000922 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200923#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000924 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200925#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000926
927 return;
928 break;
929 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400930 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000931
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000932 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
933 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
934 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100935 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000936 /* Store net params from reply */
937 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000938 dhcp_state = BOUND;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000939 printf("DHCP client bound to address %pI4\n",
940 &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000941 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000942 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000943
Simon Glasse4a3d572011-10-27 06:24:32 +0000944 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000945 return;
946 }
947 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200948 case BOUND:
949 /* DHCP client bound to address */
950 break;
wdenk3861aa52002-09-27 23:19:37 +0000951 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000952 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000953 break;
954 }
955
956}
957
958void DhcpRequest(void)
959{
960 BootpRequest();
961}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100962#endif /* CONFIG_CMD_DHCP */