blob: 87e30ab8e0a00c609110f80d901c05519c12b59f [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)
wdenk3861aa52002-09-27 23:19:37 +000044dhcp_state_t dhcp_state = INIT;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000045unsigned long dhcp_leasetime;
46IPaddr_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");
333 NetSetState(NETLOOP_FAIL);
334#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
344/*
345 * Initialize BOOTP extension fields in the request.
346 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500347#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000348static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
349 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000350{
wdenk232c1502004-03-12 00:14:09 +0000351 u8 *start = e;
352 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000353#if defined(CONFIG_BOOTP_PXE)
354 char *uuid;
355 size_t vci_strlen;
356 u16 clientarch;
357#endif
wdenk232c1502004-03-12 00:14:09 +0000358
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500359#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000360 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000361#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500362#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200363 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000364#endif
wdenk3861aa52002-09-27 23:19:37 +0000365
wdenk232c1502004-03-12 00:14:09 +0000366 *e++ = 99; /* RFC1048 Magic Cookie */
367 *e++ = 130;
368 *e++ = 83;
369 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000370
wdenk232c1502004-03-12 00:14:09 +0000371 *e++ = 53; /* DHCP Message Type */
372 *e++ = 1;
373 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000374
wdenk232c1502004-03-12 00:14:09 +0000375 *e++ = 57; /* Maximum DHCP Message Size */
376 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000377 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
378 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000379
wdenk232c1502004-03-12 00:14:09 +0000380 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000381 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000382
wdenk232c1502004-03-12 00:14:09 +0000383 *e++ = 54; /* ServerID */
384 *e++ = 4;
385 *e++ = tmp >> 24;
386 *e++ = tmp >> 16;
387 *e++ = tmp >> 8;
388 *e++ = tmp & 0xff;
389 }
wdenk3861aa52002-09-27 23:19:37 +0000390
wdenk232c1502004-03-12 00:14:09 +0000391 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000392 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000393
wdenk232c1502004-03-12 00:14:09 +0000394 *e++ = 50; /* Requested IP */
395 *e++ = 4;
396 *e++ = tmp >> 24;
397 *e++ = tmp >> 16;
398 *e++ = tmp >> 8;
399 *e++ = tmp & 0xff;
400 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500401#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000402 hostname = getenv("hostname");
403 if (hostname) {
404 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000405
406 *e++ = 12; /* Hostname */
407 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000408 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000409 e += hostnamelen;
410 }
stroesefe389a82003-08-28 14:17:32 +0000411#endif
412
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000413#if defined(CONFIG_BOOTP_PXE)
414 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
415 *e++ = 93; /* Client System Architecture */
416 *e++ = 2;
417 *e++ = (clientarch >> 8) & 0xff;
418 *e++ = clientarch & 0xff;
419
420 *e++ = 94; /* Client Network Interface Identifier */
421 *e++ = 3;
422 *e++ = 1; /* type field for UNDI */
423 *e++ = 0; /* major revision */
424 *e++ = 0; /* minor revision */
425
426 uuid = getenv("pxeuuid");
427
428 if (uuid) {
429 if (uuid_str_valid(uuid)) {
430 *e++ = 97; /* Client Machine Identifier */
431 *e++ = 17;
432 *e++ = 0; /* type 0 - UUID */
433
434 uuid_str_to_bin(uuid, e);
435 e += 16;
436 } else {
437 printf("Invalid pxeuuid: %s\n", uuid);
438 }
439 }
440
441 *e++ = 60; /* Vendor Class Identifier */
442 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
443 *e++ = vci_strlen;
444 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
445 e += vci_strlen;
446#endif
447
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500448#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000449 x = dhcp_vendorex_prep(e);
450 if (x)
wdenk232c1502004-03-12 00:14:09 +0000451 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000452#endif
453
wdenk232c1502004-03-12 00:14:09 +0000454 *e++ = 55; /* Parameter Request List */
455 cnt = e++; /* Pointer to count of requested items */
456 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500457#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000458 *e++ = 1; /* Subnet Mask */
459 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000460#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500461#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000462 *e++ = 2;
463 *cnt += 1;
464#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500465#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000466 *e++ = 3; /* Router Option */
467 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000468#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500469#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000470 *e++ = 6; /* DNS Server(s) */
471 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000472#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500473#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000474 *e++ = 12; /* Hostname */
475 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000476#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500477#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000478 *e++ = 13; /* Boot File Size */
479 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000480#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500481#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000482 *e++ = 17; /* Boot path */
483 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000484#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500485#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000486 *e++ = 40; /* NIS Domain name request */
487 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000488#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500489#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000490 *e++ = 42;
491 *cnt += 1;
492#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800493 /* no options, so back up to avoid sending an empty request list */
494 if (*cnt == 0)
495 e -= 2;
496
wdenk232c1502004-03-12 00:14:09 +0000497 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000498
wdenk232c1502004-03-12 00:14:09 +0000499 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000500#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800501 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000502 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000503#endif
504
wdenk232c1502004-03-12 00:14:09 +0000505 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000506}
507
Jon Loeliger610f2e92007-07-10 11:05:02 -0500508#else
wdenk3861aa52002-09-27 23:19:37 +0000509/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000510 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000511 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000512static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000513{
wdenk232c1502004-03-12 00:14:09 +0000514 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000515
wdenk232c1502004-03-12 00:14:09 +0000516 *e++ = 99; /* RFC1048 Magic Cookie */
517 *e++ = 130;
518 *e++ = 83;
519 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000520
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500521#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000522 *e++ = 53; /* DHCP Message Type */
523 *e++ = 1;
524 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000525
wdenk232c1502004-03-12 00:14:09 +0000526 *e++ = 57; /* Maximum DHCP Message Size */
527 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000528 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
529 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500530#endif
wdenk3861aa52002-09-27 23:19:37 +0000531
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500532#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000533 *e++ = 1; /* Subnet mask request */
534 *e++ = 4;
535 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
537
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500538#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000539 *e++ = 3; /* Default gateway request */
540 *e++ = 4;
541 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000542#endif
543
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500544#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000545 *e++ = 6; /* Domain Name Server */
546 *e++ = 4;
547 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000548#endif
549
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500550#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000551 *e++ = 12; /* Host name request */
552 *e++ = 32;
553 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000554#endif
555
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500556#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000557 *e++ = 13; /* Boot file size */
558 *e++ = 2;
559 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000560#endif
561
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500562#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000563 *e++ = 17; /* Boot path */
564 *e++ = 32;
565 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000566#endif
567
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500568#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000569 *e++ = 40; /* NIS Domain name request */
570 *e++ = 32;
571 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000572#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000573#if defined(CONFIG_BOOTP_NTPSERVER)
574 *e++ = 42;
575 *e++ = 4;
576 e += 4;
577#endif
wdenk3861aa52002-09-27 23:19:37 +0000578
wdenk232c1502004-03-12 00:14:09 +0000579 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000580
wdenk232c1502004-03-12 00:14:09 +0000581 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000582}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500583#endif
wdenk3861aa52002-09-27 23:19:37 +0000584
585void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000586BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000587{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000588 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000589 struct Bootp_t *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000590 int extlen, pktlen, iplen;
591 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000592#ifdef CONFIG_BOOTP_RANDOM_DELAY
593 ulong i, rand_ms;
594#endif
wdenk3861aa52002-09-27 23:19:37 +0000595
Simon Glass573f14f2011-12-10 11:08:06 +0000596 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500597#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000598 dhcp_state = INIT;
599#endif
600
601#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000602 if (BootpTry == 0)
603 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000604
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000605 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
606 rand_ms = rand() >> (22 - BootpTry);
607 else /* After 3rd BOOTP request max 8192 * 1ms */
608 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000609
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000610 printf("Random delay: %ld ms...\n", rand_ms);
611 for (i = 0; i < rand_ms; i++)
wdenk3861aa52002-09-27 23:19:37 +0000612 udelay(1000); /*Wait 1ms*/
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000613
wdenk3861aa52002-09-27 23:19:37 +0000614#endif /* CONFIG_BOOTP_RANDOM_DELAY */
615
616 printf("BOOTP broadcast %d\n", ++BootpTry);
617 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000618 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000619
Joe Hershbergerae446f52012-05-23 07:59:10 +0000620 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
621 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000622
623 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000624 * Next line results in incorrect packet size being transmitted,
625 * resulting in errors in some DHCP servers, reporting missing bytes.
626 * Size must be set in packet header after extension length has been
627 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000628 * C. Hallinan, DS4.COM, Inc.
629 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000630 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000631 sizeof (struct Bootp_t)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000632 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000633 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000634
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000635 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000636 bp->bp_op = OP_BOOTREQUEST;
637 bp->bp_htype = HWT_ETHER;
638 bp->bp_hlen = HWL_ETHER;
639 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200640 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000641 NetWriteIP(&bp->bp_ciaddr, 0);
642 NetWriteIP(&bp->bp_yiaddr, 0);
643 NetWriteIP(&bp->bp_siaddr, 0);
644 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000645 memcpy(bp->bp_chaddr, NetOurEther, 6);
646 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000647
648 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500649#if defined(CONFIG_CMD_DHCP)
Joe Hershbergerae446f52012-05-23 07:59:10 +0000650 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000651#else
Joe Hershbergerae446f52012-05-23 07:59:10 +0000652 extlen = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500653#endif
wdenk3861aa52002-09-27 23:19:37 +0000654
655 /*
656 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200657 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000658 */
659 BootpID = ((ulong)NetOurEther[2] << 24)
660 | ((ulong)NetOurEther[3] << 16)
661 | ((ulong)NetOurEther[4] << 8)
662 | (ulong)NetOurEther[5];
663 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000664 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000665 NetCopyLong(&bp->bp_id, &BootpID);
666
667 /*
668 * Calculate proper packet lengths taking into account the
669 * variable size of the options field
670 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000671 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
672 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000673 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200674 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000675
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500676#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000677 dhcp_state = SELECTING;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000678 net_set_udp_handler(DhcpHandler);
wdenk3861aa52002-09-27 23:19:37 +0000679#else
Joe Hershbergerece223b2012-05-23 07:59:15 +0000680 net_set_udp_handler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500681#endif
wdenk3861aa52002-09-27 23:19:37 +0000682 NetSendPacket(NetTxPacket, pktlen);
683}
684
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500685#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000686static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000687{
wdenk3e386912003-04-05 00:53:31 +0000688 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000689 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200690#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
691 int *to_ptr;
692#endif
wdenk3861aa52002-09-27 23:19:37 +0000693
wdenk232c1502004-03-12 00:14:09 +0000694 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000695 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000696 switch (*popt) {
697 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000698 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000699 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500700#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000701 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200702 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000703 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
704 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000705 break;
706#endif
wdenk232c1502004-03-12 00:14:09 +0000707 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000708 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000709 break;
710 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000711 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500712#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000713 if (*(popt + 1) > 4)
714 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000715#endif
wdenk232c1502004-03-12 00:14:09 +0000716 break;
717 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000718 size = truncate_sz("Host Name",
719 sizeof(NetOurHostName), oplen);
720 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000721 NetOurHostName[size] = 0;
722 break;
723 case 15: /* Ignore Domain Name Option */
724 break;
725 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000726 size = truncate_sz("Root Path",
727 sizeof(NetOurRootPath), oplen);
728 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000729 NetOurRootPath[size] = 0;
730 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500731#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000732 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000733 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000734 break;
735#endif
wdenk232c1502004-03-12 00:14:09 +0000736 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000737 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000738 break;
739 case 53: /* Ignore Message Type Option */
740 break;
741 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000742 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000743 break;
744 case 58: /* Ignore Renewal Time Option */
745 break;
746 case 59: /* Ignore Rebinding Time Option */
747 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100748 case 66: /* Ignore TFTP server name */
749 break;
750 case 67: /* vendor opt bootfile */
751 /*
752 * I can't use dhcp_vendorex_proc here because I need
753 * to write into the bootp packet - even then I had to
754 * pass the bootp packet pointer into here as the
755 * second arg
756 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000757 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100758 sizeof(bp->bp_file),
759 oplen);
760 if (bp->bp_file[0] == '\0' && size > 0) {
761 /*
762 * only use vendor boot file if we didn't
763 * receive a boot file in the main non-vendor
764 * part of the packet - god only knows why
765 * some vendors chose not to use this perfectly
766 * good spot to store the boot file (join on
767 * Tru64 Unix) it seems mind bogglingly crazy
768 * to me
769 */
770 printf("*** WARNING: using vendor "
771 "optional boot file\n");
772 memcpy(bp->bp_file, popt + 2, size);
773 bp->bp_file[size] = '\0';
774 }
775 break;
wdenk232c1502004-03-12 00:14:09 +0000776 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500777#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000778 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000779 break;
wdenk3861aa52002-09-27 23:19:37 +0000780#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000781 printf("*** Unhandled DHCP Option in OFFER/ACK:"
782 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000783 break;
wdenk3861aa52002-09-27 23:19:37 +0000784 }
785 popt += oplen + 2; /* Process next option */
786 }
787}
788
789static int DhcpMessageType(unsigned char *popt)
790{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000791 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000792 return -1;
793
794 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000795 while (*popt != 0xff) {
796 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000797 return *(popt + 2);
798 popt += *(popt + 1) + 2; /* Scan through all options */
799 }
800 return -1;
801}
802
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000803static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000804{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000805 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000806 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000807 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000808 int eth_hdr_size;
wdenk47cd00f2003-03-06 13:39:27 +0000809 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000810
Robin Getz0ebf04c2009-07-23 03:01:03 -0400811 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000812 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000813 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000814
Joe Hershbergerae446f52012-05-23 07:59:10 +0000815 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
816 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000817
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000818 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000819 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000820
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000821 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000822 bp->bp_op = OP_BOOTREQUEST;
823 bp->bp_htype = HWT_ETHER;
824 bp->bp_hlen = HWL_ETHER;
825 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200826 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000827 /* Do not set the client IP, your IP, or server IP yet, since it
828 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400829
Wolfgang Denkc6686702006-10-12 00:01:08 +0200830 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200831 * RFC3046 requires Relay Agents to discard packets with
832 * nonzero and offered giaddr
833 */
834 NetWriteIP(&bp->bp_giaddr, 0);
835
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000836 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000837
838 /*
839 * ID is the id of the OFFER packet
840 */
841
842 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
843
844 /*
845 * Copy options from OFFER packet if present
846 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400847
848 /* Copy offered IP into the parameters request list */
849 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000850 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
851 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000852
Joe Hershbergerae446f52012-05-23 07:59:10 +0000853 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
854 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000855 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000856
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100857#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
858 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
859#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000860 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000861 NetSendPacket(NetTxPacket, pktlen);
862}
863
864/*
865 * Handle DHCP received packets.
866 */
867static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000868DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
869 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000870{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000871 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000872
Robin Getz0ebf04c2009-07-23 03:01:03 -0400873 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000874 src, dest, len, dhcp_state);
875
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000876 /* Filter out pkts we don't want */
877 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000878 return;
879
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000880 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
881 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000882
883 switch (dhcp_state) {
884 case SELECTING:
885 /*
886 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000887 * to arrive. Then select one, and generate DHCPREQUEST
888 * response. If filename is in format we recognize, assume it
889 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000890 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400891 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200892#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000893 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200894 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000895 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200896#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000897
Robin Getz0ebf04c2009-07-23 03:01:03 -0400898 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000899 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000900
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000901 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
902 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100903 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000904
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200905 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000906 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200907#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000908 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200909#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000910
911 return;
912 break;
913 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400914 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000915
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000916 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
917 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);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000920 /* Store net params from reply */
921 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000922 dhcp_state = BOUND;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000923 printf("DHCP client bound to address %pI4\n",
924 &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000925 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000926 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000927
Simon Glasse4a3d572011-10-27 06:24:32 +0000928 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000929 return;
930 }
931 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200932 case BOUND:
933 /* DHCP client bound to address */
934 break;
wdenk3861aa52002-09-27 23:19:37 +0000935 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000936 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000937 break;
938 }
939
940}
941
942void DhcpRequest(void)
943{
944 BootpRequest();
945}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100946#endif /* CONFIG_CMD_DHCP */