blob: 0f0867b1c492dd65bbae9575c55636179f85aae5 [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"
Joe Hershbergereafc8db2012-05-23 07:57:58 +000015#include "net_rand.h"
wdenk3861aa52002-09-27 23:19:37 +000016#include "tftp.h"
wdenk232c1502004-03-12 00:14:09 +000017#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000018#ifdef CONFIG_STATUS_LED
19#include <status_led.h>
20#endif
21
Joe Hershberger3090b7e32012-05-15 08:59:06 +000022#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000023
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020024#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000025#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000026# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000027#else
wdenk232c1502004-03-12 00:14:09 +000028# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000029#endif
30
Joe Hershberger3090b7e32012-05-15 08:59:06 +000031#define PORT_BOOTPS 67 /* BOOTP server UDP port */
32#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000033
Joe Hershberger3090b7e32012-05-15 08:59:06 +000034#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000035#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000036#endif
37
38ulong BootpID;
39int BootpTry;
wdenk3861aa52002-09-27 23:19:37 +000040
Jon Loeliger643d1ab2007-07-09 17:45:14 -050041#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000042dhcp_state_t dhcp_state = INIT;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000043unsigned long dhcp_leasetime;
44IPaddr_t NetDHCPServerIP;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000045static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
46 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000047
48/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000049#if 0
50static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000051{
52 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000053 case 1: return "DHCPDISCOVER"; break;
54 case 2: return "DHCPOFFER"; break;
55 case 3: return "DHCPREQUEST"; break;
56 case 4: return "DHCPDECLINE"; break;
57 case 5: return "DHCPACK"; break;
58 case 6: return "DHCPNACK"; break;
59 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000060 default: return "UNKNOWN/INVALID MSG TYPE"; break;
61 }
62}
wdenk3e386912003-04-05 00:53:31 +000063#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050064#endif
wdenk3861aa52002-09-27 23:19:37 +000065
66static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
67{
Joe Hershberger3090b7e32012-05-15 08:59:06 +000068 struct Bootp_t *bp = (struct Bootp_t *) pkt;
wdenk3861aa52002-09-27 23:19:37 +000069 int retval = 0;
70
71 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
72 retval = -1;
Joe Hershbergerf8315732012-05-23 07:58:14 +000073 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +000074 retval = -2;
75 else if (bp->bp_op != OP_BOOTREQUEST &&
Joe Hershberger3090b7e32012-05-15 08:59:06 +000076 bp->bp_op != OP_BOOTREPLY &&
77 bp->bp_op != DHCP_OFFER &&
78 bp->bp_op != DHCP_ACK &&
79 bp->bp_op != DHCP_NAK)
wdenk3861aa52002-09-27 23:19:37 +000080 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +000081 else if (bp->bp_htype != HWT_ETHER)
82 retval = -4;
83 else if (bp->bp_hlen != HWL_ETHER)
84 retval = -5;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000085 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
wdenk3861aa52002-09-27 23:19:37 +000086 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +000087
Robin Getz0ebf04c2009-07-23 03:01:03 -040088 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000089
90 return retval;
91}
92
93/*
94 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
95 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +000096static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +000097{
Wilson Callan5d110f02007-07-28 10:56:13 -040098#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger1752f0f2012-05-23 07:59:18 +000099 IPaddr_t tmp_ip;
100
wdenk3d3befa2004-03-14 15:06:13 +0000101 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
102 if (tmp_ip != 0)
103 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershbergercb487f52012-05-23 07:58:06 +0000104 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400105#endif
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000106 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
wdenk3d3befa2004-03-14 15:06:13 +0000107 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000108 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000109
Robin Getz0ebf04c2009-07-23 03:01:03 -0400110 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000111
112 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000113 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000114 * not contain a new value
115 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000116 if (*BootFile)
117 setenv("bootfile", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000118}
119
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000120static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000121{
122 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000123 printf("*** WARNING: %s is too long (%d - max: %d)"
124 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000125 curlen = maxlen - 1;
126 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000127 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000128}
129
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500130#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000131
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000132static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000133{
wdenk232c1502004-03-12 00:14:09 +0000134 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000135
Robin Getz0ebf04c2009-07-23 03:01:03 -0400136 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000137 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000138
wdenk232c1502004-03-12 00:14:09 +0000139 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000140
wdenk232c1502004-03-12 00:14:09 +0000141 switch (*ext) {
142 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000143 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000144 if (NetOurSubnetMask == 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000145 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000146 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000147 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000148 break;
wdenk232c1502004-03-12 00:14:09 +0000149 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000150 case 3: /* Gateways list */
151 if (NetOurGatewayIP == 0)
152 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000153 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000154 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000155 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000156 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000157 break;
158 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000159 if (NetOurDNSIP == 0)
160 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500161#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000162 if ((NetOurDNS2IP == 0) && (size > 4))
163 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000164#endif
wdenk3861aa52002-09-27 23:19:37 +0000165 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000166 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000167 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000168 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000169 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000170 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000171 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000172 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000173 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000174 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000175 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000176 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000177 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000178 size = truncate_sz("Host Name",
179 sizeof(NetOurHostName), size);
180 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000181 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000182 }
183 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000184 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000185 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000186 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000187 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000188 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000189 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000190 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000192 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000193 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000194 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000195 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000196 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000197 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000198 size = truncate_sz("Root Path",
199 sizeof(NetOurRootPath), size);
200 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000201 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000202 }
203 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000204 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000205 /*
wdenk8bde7f72003-06-27 21:31:46 +0000206 * This can be used to send the information of the
207 * vendor area in another file that the client can
208 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000209 */
210 break;
wdenk232c1502004-03-12 00:14:09 +0000211 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000212 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000213 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000214 size = truncate_sz("NIS Domain Name",
215 sizeof(NetOurNISDomain), size);
216 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000217 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000218 }
219 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000220#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
221 case 42: /* NTP server IP */
222 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
223 break;
224#endif
wdenk232c1502004-03-12 00:14:09 +0000225 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000226 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000227 /*
wdenk8bde7f72003-06-27 21:31:46 +0000228 * Binary information to exchange specific
229 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000230 */
231 break;
wdenk232c1502004-03-12 00:14:09 +0000232 /* Reserved (custom) fields (128..254) */
233 }
wdenk3861aa52002-09-27 23:19:37 +0000234}
235
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000236static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000237{
wdenk232c1502004-03-12 00:14:09 +0000238 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000239
Robin Getz0ebf04c2009-07-23 03:01:03 -0400240 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000241
wdenk232c1502004-03-12 00:14:09 +0000242 while ((ext < end) && (*ext != 0xff)) {
243 if (*ext == 0) {
244 ext++;
245 } else {
246 u8 *opt = ext;
247
248 ext += ext[1] + 2;
249 if (ext <= end)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000250 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000251 }
wdenk3861aa52002-09-27 23:19:37 +0000252 }
wdenk3861aa52002-09-27 23:19:37 +0000253
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000254 debug("[BOOTP] Received fields:\n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500255 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400256 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000257
Mike Frysingerb6446b62009-02-17 00:00:53 -0500258 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400259 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000260
Robin Getz0ebf04c2009-07-23 03:01:03 -0400261 if (NetBootFileSize)
262 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000263
Robin Getz0ebf04c2009-07-23 03:01:03 -0400264 if (NetOurHostName[0])
265 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000266
Robin Getz0ebf04c2009-07-23 03:01:03 -0400267 if (NetOurRootPath[0])
268 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000269
Robin Getz0ebf04c2009-07-23 03:01:03 -0400270 if (NetOurNISDomain[0])
271 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000272
Robin Getz0ebf04c2009-07-23 03:01:03 -0400273 if (NetBootFileSize)
274 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000275
276#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
277 if (NetNtpServerIP)
278 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
279#endif
wdenk3861aa52002-09-27 23:19:37 +0000280}
Simon Glass09349862011-06-13 16:13:12 -0700281
wdenk3861aa52002-09-27 23:19:37 +0000282/*
283 * Handle a BOOTP received packet.
284 */
285static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000286BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
287 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000288{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000289 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000290
Robin Getz0ebf04c2009-07-23 03:01:03 -0400291 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000292 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000293
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000294 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000295
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000296 /* Filter out pkts we don't want */
297 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000298 return;
299
300 /*
wdenk232c1502004-03-12 00:14:09 +0000301 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000302 */
303#ifdef CONFIG_STATUS_LED
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000304 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000305#endif
306
307 BootpCopyNetParams(bp); /* Store net parameters from reply */
308
309 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000310 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200311 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000312
313 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000314 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000315
Robin Getz0ebf04c2009-07-23 03:01:03 -0400316 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000317
Simon Glasse4a3d572011-10-27 06:24:32 +0000318 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000319}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500320#endif
wdenk3861aa52002-09-27 23:19:37 +0000321
322/*
323 * Timeout on BOOTP/DHCP request.
324 */
325static void
326BootpTimeout(void)
327{
328 if (BootpTry >= TIMEOUT_COUNT) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000329#ifdef CONFIG_BOOTP_MAY_FAIL
330 puts("\nRetry count exceeded\n");
331 NetSetState(NETLOOP_FAIL);
332#else
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000333 puts("\nRetry count exceeded; starting again\n");
334 NetStartAgain();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000335#endif
wdenk3861aa52002-09-27 23:19:37 +0000336 } else {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000337 NetSetTimeout(TIMEOUT, BootpTimeout);
338 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000339 }
340}
341
342/*
343 * Initialize BOOTP extension fields in the request.
344 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500345#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000346static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
347 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000348{
wdenk232c1502004-03-12 00:14:09 +0000349 u8 *start = e;
350 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000351#if defined(CONFIG_BOOTP_PXE)
352 char *uuid;
353 size_t vci_strlen;
354 u16 clientarch;
355#endif
wdenk232c1502004-03-12 00:14:09 +0000356
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500357#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000358 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000359#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500360#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200361 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000362#endif
wdenk3861aa52002-09-27 23:19:37 +0000363
wdenk232c1502004-03-12 00:14:09 +0000364 *e++ = 99; /* RFC1048 Magic Cookie */
365 *e++ = 130;
366 *e++ = 83;
367 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000368
wdenk232c1502004-03-12 00:14:09 +0000369 *e++ = 53; /* DHCP Message Type */
370 *e++ = 1;
371 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000372
wdenk232c1502004-03-12 00:14:09 +0000373 *e++ = 57; /* Maximum DHCP Message Size */
374 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000375 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
376 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000377
wdenk232c1502004-03-12 00:14:09 +0000378 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000379 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000380
wdenk232c1502004-03-12 00:14:09 +0000381 *e++ = 54; /* ServerID */
382 *e++ = 4;
383 *e++ = tmp >> 24;
384 *e++ = tmp >> 16;
385 *e++ = tmp >> 8;
386 *e++ = tmp & 0xff;
387 }
wdenk3861aa52002-09-27 23:19:37 +0000388
wdenk232c1502004-03-12 00:14:09 +0000389 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000390 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000391
wdenk232c1502004-03-12 00:14:09 +0000392 *e++ = 50; /* Requested IP */
393 *e++ = 4;
394 *e++ = tmp >> 24;
395 *e++ = tmp >> 16;
396 *e++ = tmp >> 8;
397 *e++ = tmp & 0xff;
398 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500399#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000400 hostname = getenv("hostname");
401 if (hostname) {
402 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000403
404 *e++ = 12; /* Hostname */
405 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000406 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000407 e += hostnamelen;
408 }
stroesefe389a82003-08-28 14:17:32 +0000409#endif
410
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000411#if defined(CONFIG_BOOTP_PXE)
412 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
413 *e++ = 93; /* Client System Architecture */
414 *e++ = 2;
415 *e++ = (clientarch >> 8) & 0xff;
416 *e++ = clientarch & 0xff;
417
418 *e++ = 94; /* Client Network Interface Identifier */
419 *e++ = 3;
420 *e++ = 1; /* type field for UNDI */
421 *e++ = 0; /* major revision */
422 *e++ = 0; /* minor revision */
423
424 uuid = getenv("pxeuuid");
425
426 if (uuid) {
427 if (uuid_str_valid(uuid)) {
428 *e++ = 97; /* Client Machine Identifier */
429 *e++ = 17;
430 *e++ = 0; /* type 0 - UUID */
431
432 uuid_str_to_bin(uuid, e);
433 e += 16;
434 } else {
435 printf("Invalid pxeuuid: %s\n", uuid);
436 }
437 }
438
439 *e++ = 60; /* Vendor Class Identifier */
440 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
441 *e++ = vci_strlen;
442 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
443 e += vci_strlen;
444#endif
445
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500446#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000447 x = dhcp_vendorex_prep(e);
448 if (x)
wdenk232c1502004-03-12 00:14:09 +0000449 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000450#endif
451
wdenk232c1502004-03-12 00:14:09 +0000452 *e++ = 55; /* Parameter Request List */
453 cnt = e++; /* Pointer to count of requested items */
454 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500455#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000456 *e++ = 1; /* Subnet Mask */
457 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000458#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500459#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000460 *e++ = 2;
461 *cnt += 1;
462#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500463#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 3; /* Router Option */
465 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000466#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000468 *e++ = 6; /* DNS Server(s) */
469 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000470#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500471#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 12; /* Hostname */
473 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500475#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000476 *e++ = 13; /* Boot File Size */
477 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000478#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500479#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000480 *e++ = 17; /* Boot path */
481 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000482#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500483#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000484 *e++ = 40; /* NIS Domain name request */
485 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000486#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500487#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000488 *e++ = 42;
489 *cnt += 1;
490#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800491 /* no options, so back up to avoid sending an empty request list */
492 if (*cnt == 0)
493 e -= 2;
494
wdenk232c1502004-03-12 00:14:09 +0000495 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000496
wdenk232c1502004-03-12 00:14:09 +0000497 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000498#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800499 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000500 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000501#endif
502
wdenk232c1502004-03-12 00:14:09 +0000503 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000504}
505
Jon Loeliger610f2e92007-07-10 11:05:02 -0500506#else
wdenk3861aa52002-09-27 23:19:37 +0000507/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000508 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000509 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000510static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000511{
wdenk232c1502004-03-12 00:14:09 +0000512 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000513
wdenk232c1502004-03-12 00:14:09 +0000514 *e++ = 99; /* RFC1048 Magic Cookie */
515 *e++ = 130;
516 *e++ = 83;
517 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000518
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500519#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000520 *e++ = 53; /* DHCP Message Type */
521 *e++ = 1;
522 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000523
wdenk232c1502004-03-12 00:14:09 +0000524 *e++ = 57; /* Maximum DHCP Message Size */
525 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000526 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
527 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500528#endif
wdenk3861aa52002-09-27 23:19:37 +0000529
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500530#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000531 *e++ = 1; /* Subnet mask request */
532 *e++ = 4;
533 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000534#endif
535
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500536#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000537 *e++ = 3; /* Default gateway request */
538 *e++ = 4;
539 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000540#endif
541
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500542#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000543 *e++ = 6; /* Domain Name Server */
544 *e++ = 4;
545 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000546#endif
547
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500548#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000549 *e++ = 12; /* Host name request */
550 *e++ = 32;
551 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000552#endif
553
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500554#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000555 *e++ = 13; /* Boot file size */
556 *e++ = 2;
557 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000558#endif
559
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500560#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000561 *e++ = 17; /* Boot path */
562 *e++ = 32;
563 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000564#endif
565
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500566#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000567 *e++ = 40; /* NIS Domain name request */
568 *e++ = 32;
569 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000570#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000571#if defined(CONFIG_BOOTP_NTPSERVER)
572 *e++ = 42;
573 *e++ = 4;
574 e += 4;
575#endif
wdenk3861aa52002-09-27 23:19:37 +0000576
wdenk232c1502004-03-12 00:14:09 +0000577 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000578
wdenk232c1502004-03-12 00:14:09 +0000579 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000580}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500581#endif
wdenk3861aa52002-09-27 23:19:37 +0000582
583void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000584BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000585{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000586 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000587 struct Bootp_t *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000588 int extlen, pktlen, iplen;
589 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000590#ifdef CONFIG_BOOTP_RANDOM_DELAY
591 ulong i, rand_ms;
592#endif
wdenk3861aa52002-09-27 23:19:37 +0000593
Simon Glass573f14f2011-12-10 11:08:06 +0000594 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500595#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000596 dhcp_state = INIT;
597#endif
598
599#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000600 if (BootpTry == 0)
601 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000602
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000603 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
604 rand_ms = rand() >> (22 - BootpTry);
605 else /* After 3rd BOOTP request max 8192 * 1ms */
606 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000607
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000608 printf("Random delay: %ld ms...\n", rand_ms);
609 for (i = 0; i < rand_ms; i++)
wdenk3861aa52002-09-27 23:19:37 +0000610 udelay(1000); /*Wait 1ms*/
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000611
wdenk3861aa52002-09-27 23:19:37 +0000612#endif /* CONFIG_BOOTP_RANDOM_DELAY */
613
614 printf("BOOTP broadcast %d\n", ++BootpTry);
615 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000616 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000617
Joe Hershbergerae446f52012-05-23 07:59:10 +0000618 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
619 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000620
621 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000622 * Next line results in incorrect packet size being transmitted,
623 * resulting in errors in some DHCP servers, reporting missing bytes.
624 * Size must be set in packet header after extension length has been
625 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000626 * C. Hallinan, DS4.COM, Inc.
627 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000628 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000629 sizeof (struct Bootp_t)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000630 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000631 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000632
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000633 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000634 bp->bp_op = OP_BOOTREQUEST;
635 bp->bp_htype = HWT_ETHER;
636 bp->bp_hlen = HWL_ETHER;
637 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200638 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000639 NetWriteIP(&bp->bp_ciaddr, 0);
640 NetWriteIP(&bp->bp_yiaddr, 0);
641 NetWriteIP(&bp->bp_siaddr, 0);
642 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000643 memcpy(bp->bp_chaddr, NetOurEther, 6);
644 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000645
646 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500647#if defined(CONFIG_CMD_DHCP)
Joe Hershbergerae446f52012-05-23 07:59:10 +0000648 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000649#else
Joe Hershbergerae446f52012-05-23 07:59:10 +0000650 extlen = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500651#endif
wdenk3861aa52002-09-27 23:19:37 +0000652
653 /*
654 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200655 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000656 */
657 BootpID = ((ulong)NetOurEther[2] << 24)
658 | ((ulong)NetOurEther[3] << 16)
659 | ((ulong)NetOurEther[4] << 8)
660 | (ulong)NetOurEther[5];
661 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000662 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000663 NetCopyLong(&bp->bp_id, &BootpID);
664
665 /*
666 * Calculate proper packet lengths taking into account the
667 * variable size of the options field
668 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000669 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
670 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000671 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200672 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000673
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500674#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000675 dhcp_state = SELECTING;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000676 net_set_udp_handler(DhcpHandler);
wdenk3861aa52002-09-27 23:19:37 +0000677#else
Joe Hershbergerece223b2012-05-23 07:59:15 +0000678 net_set_udp_handler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500679#endif
wdenk3861aa52002-09-27 23:19:37 +0000680 NetSendPacket(NetTxPacket, pktlen);
681}
682
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500683#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000684static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000685{
wdenk3e386912003-04-05 00:53:31 +0000686 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000687 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200688#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
689 int *to_ptr;
690#endif
wdenk3861aa52002-09-27 23:19:37 +0000691
wdenk232c1502004-03-12 00:14:09 +0000692 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000693 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000694 switch (*popt) {
695 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000696 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000697 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500698#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000699 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200700 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000701 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
702 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000703 break;
704#endif
wdenk232c1502004-03-12 00:14:09 +0000705 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000706 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000707 break;
708 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000709 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500710#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000711 if (*(popt + 1) > 4)
712 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000713#endif
wdenk232c1502004-03-12 00:14:09 +0000714 break;
715 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000716 size = truncate_sz("Host Name",
717 sizeof(NetOurHostName), oplen);
718 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000719 NetOurHostName[size] = 0;
720 break;
721 case 15: /* Ignore Domain Name Option */
722 break;
723 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000724 size = truncate_sz("Root Path",
725 sizeof(NetOurRootPath), oplen);
726 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000727 NetOurRootPath[size] = 0;
728 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500729#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000730 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000731 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000732 break;
733#endif
wdenk232c1502004-03-12 00:14:09 +0000734 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000735 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000736 break;
737 case 53: /* Ignore Message Type Option */
738 break;
739 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000740 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000741 break;
742 case 58: /* Ignore Renewal Time Option */
743 break;
744 case 59: /* Ignore Rebinding Time Option */
745 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100746 case 66: /* Ignore TFTP server name */
747 break;
748 case 67: /* vendor opt bootfile */
749 /*
750 * I can't use dhcp_vendorex_proc here because I need
751 * to write into the bootp packet - even then I had to
752 * pass the bootp packet pointer into here as the
753 * second arg
754 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000755 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100756 sizeof(bp->bp_file),
757 oplen);
758 if (bp->bp_file[0] == '\0' && size > 0) {
759 /*
760 * only use vendor boot file if we didn't
761 * receive a boot file in the main non-vendor
762 * part of the packet - god only knows why
763 * some vendors chose not to use this perfectly
764 * good spot to store the boot file (join on
765 * Tru64 Unix) it seems mind bogglingly crazy
766 * to me
767 */
768 printf("*** WARNING: using vendor "
769 "optional boot file\n");
770 memcpy(bp->bp_file, popt + 2, size);
771 bp->bp_file[size] = '\0';
772 }
773 break;
wdenk232c1502004-03-12 00:14:09 +0000774 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500775#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000776 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000777 break;
wdenk3861aa52002-09-27 23:19:37 +0000778#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000779 printf("*** Unhandled DHCP Option in OFFER/ACK:"
780 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000781 break;
wdenk3861aa52002-09-27 23:19:37 +0000782 }
783 popt += oplen + 2; /* Process next option */
784 }
785}
786
787static int DhcpMessageType(unsigned char *popt)
788{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000789 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000790 return -1;
791
792 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000793 while (*popt != 0xff) {
794 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000795 return *(popt + 2);
796 popt += *(popt + 1) + 2; /* Scan through all options */
797 }
798 return -1;
799}
800
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000801static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000802{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000803 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000804 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000805 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000806 int eth_hdr_size;
wdenk47cd00f2003-03-06 13:39:27 +0000807 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000808
Robin Getz0ebf04c2009-07-23 03:01:03 -0400809 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000810 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000811 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000812
Joe Hershbergerae446f52012-05-23 07:59:10 +0000813 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
814 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000815
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000816 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000817 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000818
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000819 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000820 bp->bp_op = OP_BOOTREQUEST;
821 bp->bp_htype = HWT_ETHER;
822 bp->bp_hlen = HWL_ETHER;
823 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200824 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000825 /* Do not set the client IP, your IP, or server IP yet, since it
826 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400827
Wolfgang Denkc6686702006-10-12 00:01:08 +0200828 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200829 * RFC3046 requires Relay Agents to discard packets with
830 * nonzero and offered giaddr
831 */
832 NetWriteIP(&bp->bp_giaddr, 0);
833
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000834 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000835
836 /*
837 * ID is the id of the OFFER packet
838 */
839
840 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
841
842 /*
843 * Copy options from OFFER packet if present
844 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400845
846 /* Copy offered IP into the parameters request list */
847 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000848 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
849 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000850
Joe Hershbergerae446f52012-05-23 07:59:10 +0000851 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
852 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000853 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000854
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100855#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
856 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
857#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000858 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000859 NetSendPacket(NetTxPacket, pktlen);
860}
861
862/*
863 * Handle DHCP received packets.
864 */
865static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000866DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
867 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000868{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000869 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000870
Robin Getz0ebf04c2009-07-23 03:01:03 -0400871 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000872 src, dest, len, dhcp_state);
873
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000874 /* Filter out pkts we don't want */
875 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000876 return;
877
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000878 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
879 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000880
881 switch (dhcp_state) {
882 case SELECTING:
883 /*
884 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000885 * to arrive. Then select one, and generate DHCPREQUEST
886 * response. If filename is in format we recognize, assume it
887 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000888 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400889 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200890#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000891 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200892 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000893 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200894#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000895
Robin Getz0ebf04c2009-07-23 03:01:03 -0400896 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000897 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000898
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000899 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
900 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100901 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000902
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200903 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000904 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200905#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000906 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200907#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000908
909 return;
910 break;
911 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400912 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000913
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000914 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
915 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
916 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100917 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000918 /* Store net params from reply */
919 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000920 dhcp_state = BOUND;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000921 printf("DHCP client bound to address %pI4\n",
922 &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000923 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000924 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000925
Simon Glasse4a3d572011-10-27 06:24:32 +0000926 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000927 return;
928 }
929 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200930 case BOUND:
931 /* DHCP client bound to address */
932 break;
wdenk3861aa52002-09-27 23:19:37 +0000933 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000934 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000935 break;
936 }
937
938}
939
940void DhcpRequest(void)
941{
942 BootpRequest();
943}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100944#endif /* CONFIG_CMD_DHCP */