blob: d0a7da248e4e9c57251548bbafb82b5568384698 [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
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +000020#include <linux/compiler.h>
wdenk3861aa52002-09-27 23:19:37 +000021
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;
40#ifdef CONFIG_BOOTP_RANDOM_DELAY
41ulong seed1, seed2;
42#endif
43
Jon Loeliger643d1ab2007-07-09 17:45:14 -050044#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000045dhcp_state_t dhcp_state = INIT;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000046unsigned long dhcp_leasetime;
47IPaddr_t NetDHCPServerIP;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000048static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
49 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000050
51/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000052#if 0
53static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000054{
55 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000056 case 1: return "DHCPDISCOVER"; break;
57 case 2: return "DHCPOFFER"; break;
58 case 3: return "DHCPREQUEST"; break;
59 case 4: return "DHCPDECLINE"; break;
60 case 5: return "DHCPACK"; break;
61 case 6: return "DHCPNACK"; break;
62 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000063 default: return "UNKNOWN/INVALID MSG TYPE"; break;
64 }
65}
wdenk3e386912003-04-05 00:53:31 +000066#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050067#endif
wdenk3861aa52002-09-27 23:19:37 +000068
69static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
70{
Joe Hershberger3090b7e32012-05-15 08:59:06 +000071 struct Bootp_t *bp = (struct Bootp_t *) pkt;
wdenk3861aa52002-09-27 23:19:37 +000072 int retval = 0;
73
74 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
75 retval = -1;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000076 else if (len < sizeof(struct Bootp_t) - OPT_SIZE)
wdenk3861aa52002-09-27 23:19:37 +000077 retval = -2;
78 else if (bp->bp_op != OP_BOOTREQUEST &&
Joe Hershberger3090b7e32012-05-15 08:59:06 +000079 bp->bp_op != OP_BOOTREPLY &&
80 bp->bp_op != DHCP_OFFER &&
81 bp->bp_op != DHCP_ACK &&
82 bp->bp_op != DHCP_NAK)
wdenk3861aa52002-09-27 23:19:37 +000083 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +000084 else if (bp->bp_htype != HWT_ETHER)
85 retval = -4;
86 else if (bp->bp_hlen != HWL_ETHER)
87 retval = -5;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000088 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
wdenk3861aa52002-09-27 23:19:37 +000089 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +000090
Robin Getz0ebf04c2009-07-23 03:01:03 -040091 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000092
93 return retval;
94}
95
96/*
97 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
98 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +000099static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000100{
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +0000101 __maybe_unused IPaddr_t tmp_ip;
wdenk3d3befa2004-03-14 15:06:13 +0000102
wdenk3861aa52002-09-27 23:19:37 +0000103 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400104#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000105 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
106 if (tmp_ip != 0)
107 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000108 memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400109#endif
wdenk3d3befa2004-03-14 15:06:13 +0000110 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000111 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000112
Robin Getz0ebf04c2009-07-23 03:01:03 -0400113 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000114
115 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000116 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000117 * not contain a new value
118 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000119 if (*BootFile)
120 setenv("bootfile", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000121}
122
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000123static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000124{
125 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000126 printf("*** WARNING: %s is too long (%d - max: %d)"
127 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000128 curlen = maxlen - 1;
129 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000130 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000131}
132
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500133#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000134
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000135static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000136{
wdenk232c1502004-03-12 00:14:09 +0000137 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000138
Robin Getz0ebf04c2009-07-23 03:01:03 -0400139 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000140 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000141
wdenk232c1502004-03-12 00:14:09 +0000142 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000143
wdenk232c1502004-03-12 00:14:09 +0000144 switch (*ext) {
145 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000146 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000147 if (NetOurSubnetMask == 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000148 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000149 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000150 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000151 break;
wdenk232c1502004-03-12 00:14:09 +0000152 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000153 case 3: /* Gateways list */
154 if (NetOurGatewayIP == 0)
155 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000156 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000157 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000158 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000159 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000160 break;
161 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000162 if (NetOurDNSIP == 0)
163 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500164#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000165 if ((NetOurDNS2IP == 0) && (size > 4))
166 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000167#endif
wdenk3861aa52002-09-27 23:19:37 +0000168 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000169 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000170 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000171 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000172 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000173 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000174 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000175 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000176 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000177 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000178 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000179 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000180 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000181 size = truncate_sz("Host Name",
182 sizeof(NetOurHostName), size);
183 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000184 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000185 }
186 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000187 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000188 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000189 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000190 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000191 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000192 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000193 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000194 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000195 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000196 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000197 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000198 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000199 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000200 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000201 size = truncate_sz("Root Path",
202 sizeof(NetOurRootPath), size);
203 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000204 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000205 }
206 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000207 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000208 /*
wdenk8bde7f72003-06-27 21:31:46 +0000209 * This can be used to send the information of the
210 * vendor area in another file that the client can
211 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000212 */
213 break;
wdenk232c1502004-03-12 00:14:09 +0000214 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000215 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000216 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000217 size = truncate_sz("NIS Domain Name",
218 sizeof(NetOurNISDomain), size);
219 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000220 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000221 }
222 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000223#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
224 case 42: /* NTP server IP */
225 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
226 break;
227#endif
wdenk232c1502004-03-12 00:14:09 +0000228 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000229 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000230 /*
wdenk8bde7f72003-06-27 21:31:46 +0000231 * Binary information to exchange specific
232 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000233 */
234 break;
wdenk232c1502004-03-12 00:14:09 +0000235 /* Reserved (custom) fields (128..254) */
236 }
wdenk3861aa52002-09-27 23:19:37 +0000237}
238
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000239static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000240{
wdenk232c1502004-03-12 00:14:09 +0000241 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000242
Robin Getz0ebf04c2009-07-23 03:01:03 -0400243 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000244
wdenk232c1502004-03-12 00:14:09 +0000245 while ((ext < end) && (*ext != 0xff)) {
246 if (*ext == 0) {
247 ext++;
248 } else {
249 u8 *opt = ext;
250
251 ext += ext[1] + 2;
252 if (ext <= end)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000253 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000254 }
wdenk3861aa52002-09-27 23:19:37 +0000255 }
wdenk3861aa52002-09-27 23:19:37 +0000256
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000257 debug("[BOOTP] Received fields:\n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500258 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400259 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000260
Mike Frysingerb6446b62009-02-17 00:00:53 -0500261 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400262 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000263
Robin Getz0ebf04c2009-07-23 03:01:03 -0400264 if (NetBootFileSize)
265 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000266
Robin Getz0ebf04c2009-07-23 03:01:03 -0400267 if (NetOurHostName[0])
268 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000269
Robin Getz0ebf04c2009-07-23 03:01:03 -0400270 if (NetOurRootPath[0])
271 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000272
Robin Getz0ebf04c2009-07-23 03:01:03 -0400273 if (NetOurNISDomain[0])
274 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000275
Robin Getz0ebf04c2009-07-23 03:01:03 -0400276 if (NetBootFileSize)
277 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000278
279#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
280 if (NetNtpServerIP)
281 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
282#endif
wdenk3861aa52002-09-27 23:19:37 +0000283}
Simon Glass09349862011-06-13 16:13:12 -0700284
wdenk3861aa52002-09-27 23:19:37 +0000285/*
286 * Handle a BOOTP received packet.
287 */
288static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000289BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
290 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000291{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000292 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000293
Robin Getz0ebf04c2009-07-23 03:01:03 -0400294 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000295 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000296
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000297 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000298
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000299 /* Filter out pkts we don't want */
300 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000301 return;
302
303 /*
wdenk232c1502004-03-12 00:14:09 +0000304 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000305 */
306#ifdef CONFIG_STATUS_LED
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000307 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000308#endif
309
310 BootpCopyNetParams(bp); /* Store net parameters from reply */
311
312 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000313 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200314 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000315
316 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000317 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000318
Robin Getz0ebf04c2009-07-23 03:01:03 -0400319 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000320
Simon Glasse4a3d572011-10-27 06:24:32 +0000321 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000322}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500323#endif
wdenk3861aa52002-09-27 23:19:37 +0000324
325/*
326 * Timeout on BOOTP/DHCP request.
327 */
328static void
329BootpTimeout(void)
330{
331 if (BootpTry >= TIMEOUT_COUNT) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000332 puts("\nRetry count exceeded; starting again\n");
333 NetStartAgain();
wdenk3861aa52002-09-27 23:19:37 +0000334 } else {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000335 NetSetTimeout(TIMEOUT, BootpTimeout);
336 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000337 }
338}
339
340/*
341 * Initialize BOOTP extension fields in the request.
342 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500343#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000344static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
345 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000346{
wdenk232c1502004-03-12 00:14:09 +0000347 u8 *start = e;
348 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000349#if defined(CONFIG_BOOTP_PXE)
350 char *uuid;
351 size_t vci_strlen;
352 u16 clientarch;
353#endif
wdenk232c1502004-03-12 00:14:09 +0000354
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500355#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000356 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000357#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500358#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200359 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000360#endif
wdenk3861aa52002-09-27 23:19:37 +0000361
wdenk232c1502004-03-12 00:14:09 +0000362 *e++ = 99; /* RFC1048 Magic Cookie */
363 *e++ = 130;
364 *e++ = 83;
365 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000366
wdenk232c1502004-03-12 00:14:09 +0000367 *e++ = 53; /* DHCP Message Type */
368 *e++ = 1;
369 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000370
wdenk232c1502004-03-12 00:14:09 +0000371 *e++ = 57; /* Maximum DHCP Message Size */
372 *e++ = 2;
373 *e++ = (576 - 312 + OPT_SIZE) >> 8;
374 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000375
wdenk232c1502004-03-12 00:14:09 +0000376 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000377 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000378
wdenk232c1502004-03-12 00:14:09 +0000379 *e++ = 54; /* ServerID */
380 *e++ = 4;
381 *e++ = tmp >> 24;
382 *e++ = tmp >> 16;
383 *e++ = tmp >> 8;
384 *e++ = tmp & 0xff;
385 }
wdenk3861aa52002-09-27 23:19:37 +0000386
wdenk232c1502004-03-12 00:14:09 +0000387 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000388 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000389
wdenk232c1502004-03-12 00:14:09 +0000390 *e++ = 50; /* Requested IP */
391 *e++ = 4;
392 *e++ = tmp >> 24;
393 *e++ = tmp >> 16;
394 *e++ = tmp >> 8;
395 *e++ = tmp & 0xff;
396 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500397#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000398 hostname = getenv("hostname");
399 if (hostname) {
400 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000401
402 *e++ = 12; /* Hostname */
403 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000404 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000405 e += hostnamelen;
406 }
stroesefe389a82003-08-28 14:17:32 +0000407#endif
408
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000409#if defined(CONFIG_BOOTP_PXE)
410 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
411 *e++ = 93; /* Client System Architecture */
412 *e++ = 2;
413 *e++ = (clientarch >> 8) & 0xff;
414 *e++ = clientarch & 0xff;
415
416 *e++ = 94; /* Client Network Interface Identifier */
417 *e++ = 3;
418 *e++ = 1; /* type field for UNDI */
419 *e++ = 0; /* major revision */
420 *e++ = 0; /* minor revision */
421
422 uuid = getenv("pxeuuid");
423
424 if (uuid) {
425 if (uuid_str_valid(uuid)) {
426 *e++ = 97; /* Client Machine Identifier */
427 *e++ = 17;
428 *e++ = 0; /* type 0 - UUID */
429
430 uuid_str_to_bin(uuid, e);
431 e += 16;
432 } else {
433 printf("Invalid pxeuuid: %s\n", uuid);
434 }
435 }
436
437 *e++ = 60; /* Vendor Class Identifier */
438 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
439 *e++ = vci_strlen;
440 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
441 e += vci_strlen;
442#endif
443
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500444#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000445 x = dhcp_vendorex_prep(e);
446 if (x)
wdenk232c1502004-03-12 00:14:09 +0000447 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000448#endif
449
wdenk232c1502004-03-12 00:14:09 +0000450 *e++ = 55; /* Parameter Request List */
451 cnt = e++; /* Pointer to count of requested items */
452 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500453#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000454 *e++ = 1; /* Subnet Mask */
455 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000456#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500457#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000458 *e++ = 2;
459 *cnt += 1;
460#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500461#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000462 *e++ = 3; /* Router Option */
463 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000464#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500465#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000466 *e++ = 6; /* DNS Server(s) */
467 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000468#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500469#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000470 *e++ = 12; /* Hostname */
471 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000472#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500473#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000474 *e++ = 13; /* Boot File Size */
475 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000476#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500477#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000478 *e++ = 17; /* Boot path */
479 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000480#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500481#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000482 *e++ = 40; /* NIS Domain name request */
483 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000484#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500485#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000486 *e++ = 42;
487 *cnt += 1;
488#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800489 /* no options, so back up to avoid sending an empty request list */
490 if (*cnt == 0)
491 e -= 2;
492
wdenk232c1502004-03-12 00:14:09 +0000493 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000494
wdenk232c1502004-03-12 00:14:09 +0000495 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000496#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800497 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000498 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000499#endif
500
wdenk232c1502004-03-12 00:14:09 +0000501 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000502}
503
Jon Loeliger610f2e92007-07-10 11:05:02 -0500504#else
wdenk3861aa52002-09-27 23:19:37 +0000505/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000506 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000507 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000508static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000509{
wdenk232c1502004-03-12 00:14:09 +0000510 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000511
wdenk232c1502004-03-12 00:14:09 +0000512 *e++ = 99; /* RFC1048 Magic Cookie */
513 *e++ = 130;
514 *e++ = 83;
515 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000516
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500517#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000518 *e++ = 53; /* DHCP Message Type */
519 *e++ = 1;
520 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000521
wdenk232c1502004-03-12 00:14:09 +0000522 *e++ = 57; /* Maximum DHCP Message Size */
523 *e++ = 2;
524 *e++ = (576 - 312 + OPT_SIZE) >> 16;
525 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500526#endif
wdenk3861aa52002-09-27 23:19:37 +0000527
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500528#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000529 *e++ = 1; /* Subnet mask request */
530 *e++ = 4;
531 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000532#endif
533
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500534#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000535 *e++ = 3; /* Default gateway request */
536 *e++ = 4;
537 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000538#endif
539
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500540#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000541 *e++ = 6; /* Domain Name Server */
542 *e++ = 4;
543 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000544#endif
545
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500546#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000547 *e++ = 12; /* Host name request */
548 *e++ = 32;
549 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000550#endif
551
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500552#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000553 *e++ = 13; /* Boot file size */
554 *e++ = 2;
555 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000556#endif
557
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500558#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000559 *e++ = 17; /* Boot path */
560 *e++ = 32;
561 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000562#endif
563
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500564#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000565 *e++ = 40; /* NIS Domain name request */
566 *e++ = 32;
567 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000568#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000569#if defined(CONFIG_BOOTP_NTPSERVER)
570 *e++ = 42;
571 *e++ = 4;
572 e += 4;
573#endif
wdenk3861aa52002-09-27 23:19:37 +0000574
wdenk232c1502004-03-12 00:14:09 +0000575 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000576
wdenk232c1502004-03-12 00:14:09 +0000577 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000578}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500579#endif
wdenk3861aa52002-09-27 23:19:37 +0000580
581void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000582BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000583{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000584 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000585 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000586 int ext_len, pktlen, iplen;
587
Simon Glass573f14f2011-12-10 11:08:06 +0000588 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500589#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000590 dhcp_state = INIT;
591#endif
592
593#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
594 unsigned char bi_enetaddr[6];
595 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000596 ulong tst1, tst2, sum, m_mask, m_value = 0;
597
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000598 if (BootpTry == 0) {
wdenk3861aa52002-09-27 23:19:37 +0000599 /* get our mac */
Mike Frysinger95823ca2009-02-11 18:23:48 -0500600 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000601
Robin Getz0ebf04c2009-07-23 03:01:03 -0400602 debug("BootpRequest => Our Mac: ");
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000603 for (reg = 0; reg < 6; reg++)
604 debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000605
606 /* Mac-Manipulation 2 get seed1 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000607 tst1 = 0;
608 tst2 = 0;
609 for (reg = 2; reg < 6; reg++) {
wdenk3861aa52002-09-27 23:19:37 +0000610 tst1 = tst1 << 8;
611 tst1 = tst1 | bi_enetaddr[reg];
612 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000613 for (reg = 0; reg < 2; reg++) {
wdenk3861aa52002-09-27 23:19:37 +0000614 tst2 = tst2 | bi_enetaddr[reg];
615 tst2 = tst2 << 8;
616 }
617
618 seed1 = tst1^tst2;
619
620 /* Mirror seed1*/
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000621 m_mask = 0x1;
622 for (reg = 1; reg <= 32; reg++) {
wdenk3861aa52002-09-27 23:19:37 +0000623 m_value |= (m_mask & seed1);
624 seed1 = seed1 >> 1;
625 m_value = m_value << 1;
626 }
627 seed1 = m_value;
628 seed2 = 0xB78D0945;
629 }
630
631 /* Random Number Generator */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000632 for (reg = 0; reg <= 0; reg++) {
wdenk3861aa52002-09-27 23:19:37 +0000633 sum = seed1 + seed2;
634 if (sum < seed1 || sum < seed2)
635 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000636 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000637 seed1 = sum;
638
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000639 if (BootpTry <= 2) { /* Start with max 1024 * 1ms */
wdenk3861aa52002-09-27 23:19:37 +0000640 sum = sum >> (22-BootpTry);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000641 } else { /*After 3rd BOOTP request max 8192 * 1ms */
wdenk3861aa52002-09-27 23:19:37 +0000642 sum = sum >> 19;
643 }
644 }
645
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000646 printf("Random delay: %ld ms...\n", sum);
647 for (reg = 0; reg < sum; reg++)
wdenk3861aa52002-09-27 23:19:37 +0000648 udelay(1000); /*Wait 1ms*/
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000649
wdenk3861aa52002-09-27 23:19:37 +0000650#endif /* CONFIG_BOOTP_RANDOM_DELAY */
651
652 printf("BOOTP broadcast %d\n", ++BootpTry);
653 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000654 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000655
wdenka3d991b2004-04-15 21:48:45 +0000656 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000657
658 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000659 * Next line results in incorrect packet size being transmitted,
660 * resulting in errors in some DHCP servers, reporting missing bytes.
661 * Size must be set in packet header after extension length has been
662 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000663 * C. Hallinan, DS4.COM, Inc.
664 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000665 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
666 sizeof (struct Bootp_t)); */
wdenk3861aa52002-09-27 23:19:37 +0000667 iphdr = pkt; /* We need this later for NetSetIP() */
668 pkt += IP_HDR_SIZE;
669
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000670 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000671 bp->bp_op = OP_BOOTREQUEST;
672 bp->bp_htype = HWT_ETHER;
673 bp->bp_hlen = HWL_ETHER;
674 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200675 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000676 NetWriteIP(&bp->bp_ciaddr, 0);
677 NetWriteIP(&bp->bp_yiaddr, 0);
678 NetWriteIP(&bp->bp_siaddr, 0);
679 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000680 memcpy(bp->bp_chaddr, NetOurEther, 6);
681 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000682
683 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500684#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200685 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000686#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200687 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500688#endif
wdenk3861aa52002-09-27 23:19:37 +0000689
690 /*
691 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200692 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000693 */
694 BootpID = ((ulong)NetOurEther[2] << 24)
695 | ((ulong)NetOurEther[3] << 16)
696 | ((ulong)NetOurEther[4] << 8)
697 | (ulong)NetOurEther[5];
698 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000699 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000700 NetCopyLong(&bp->bp_id, &BootpID);
701
702 /*
703 * Calculate proper packet lengths taking into account the
704 * variable size of the options field
705 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000706 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
707 sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000708 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
709 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200710 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000711
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500712#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000713 dhcp_state = SELECTING;
714 NetSetHandler(DhcpHandler);
715#else
716 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500717#endif
wdenk3861aa52002-09-27 23:19:37 +0000718 NetSendPacket(NetTxPacket, pktlen);
719}
720
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500721#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000722static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000723{
wdenk3e386912003-04-05 00:53:31 +0000724 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000725 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200726#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
727 int *to_ptr;
728#endif
wdenk3861aa52002-09-27 23:19:37 +0000729
wdenk232c1502004-03-12 00:14:09 +0000730 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000731 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000732 switch (*popt) {
733 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000734 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000735 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500736#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000737 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200738 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000739 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
740 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000741 break;
742#endif
wdenk232c1502004-03-12 00:14:09 +0000743 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000744 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000745 break;
746 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000747 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500748#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000749 if (*(popt + 1) > 4)
750 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000751#endif
wdenk232c1502004-03-12 00:14:09 +0000752 break;
753 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000754 size = truncate_sz("Host Name",
755 sizeof(NetOurHostName), oplen);
756 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000757 NetOurHostName[size] = 0;
758 break;
759 case 15: /* Ignore Domain Name Option */
760 break;
761 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000762 size = truncate_sz("Root Path",
763 sizeof(NetOurRootPath), oplen);
764 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000765 NetOurRootPath[size] = 0;
766 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500767#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000768 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000769 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000770 break;
771#endif
wdenk232c1502004-03-12 00:14:09 +0000772 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000773 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000774 break;
775 case 53: /* Ignore Message Type Option */
776 break;
777 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000778 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000779 break;
780 case 58: /* Ignore Renewal Time Option */
781 break;
782 case 59: /* Ignore Rebinding Time Option */
783 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100784 case 66: /* Ignore TFTP server name */
785 break;
786 case 67: /* vendor opt bootfile */
787 /*
788 * I can't use dhcp_vendorex_proc here because I need
789 * to write into the bootp packet - even then I had to
790 * pass the bootp packet pointer into here as the
791 * second arg
792 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000793 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100794 sizeof(bp->bp_file),
795 oplen);
796 if (bp->bp_file[0] == '\0' && size > 0) {
797 /*
798 * only use vendor boot file if we didn't
799 * receive a boot file in the main non-vendor
800 * part of the packet - god only knows why
801 * some vendors chose not to use this perfectly
802 * good spot to store the boot file (join on
803 * Tru64 Unix) it seems mind bogglingly crazy
804 * to me
805 */
806 printf("*** WARNING: using vendor "
807 "optional boot file\n");
808 memcpy(bp->bp_file, popt + 2, size);
809 bp->bp_file[size] = '\0';
810 }
811 break;
wdenk232c1502004-03-12 00:14:09 +0000812 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500813#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000814 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000815 break;
wdenk3861aa52002-09-27 23:19:37 +0000816#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000817 printf("*** Unhandled DHCP Option in OFFER/ACK:"
818 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000819 break;
wdenk3861aa52002-09-27 23:19:37 +0000820 }
821 popt += oplen + 2; /* Process next option */
822 }
823}
824
825static int DhcpMessageType(unsigned char *popt)
826{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000827 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000828 return -1;
829
830 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000831 while (*popt != 0xff) {
832 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000833 return *(popt + 2);
834 popt += *(popt + 1) + 2; /* Scan through all options */
835 }
836 return -1;
837}
838
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000839static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000840{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000841 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000842 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000843 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000844 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000845
Robin Getz0ebf04c2009-07-23 03:01:03 -0400846 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000847 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000848 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000849
wdenka3d991b2004-04-15 21:48:45 +0000850 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000851
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000852 iphdr = pkt; /* We'll need this later to set proper pkt size */
wdenk3861aa52002-09-27 23:19:37 +0000853 pkt += IP_HDR_SIZE;
854
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000855 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000856 bp->bp_op = OP_BOOTREQUEST;
857 bp->bp_htype = HWT_ETHER;
858 bp->bp_hlen = HWL_ETHER;
859 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200860 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000861 /* Do not set the client IP, your IP, or server IP yet, since it
862 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400863
Wolfgang Denkc6686702006-10-12 00:01:08 +0200864 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200865 * RFC3046 requires Relay Agents to discard packets with
866 * nonzero and offered giaddr
867 */
868 NetWriteIP(&bp->bp_giaddr, 0);
869
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000870 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000871
872 /*
873 * ID is the id of the OFFER packet
874 */
875
876 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
877
878 /*
879 * Copy options from OFFER packet if present
880 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400881
882 /* Copy offered IP into the parameters request list */
883 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000884 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
885 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000886
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000887 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
888 sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000889 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
890 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
891
Robin Getz0ebf04c2009-07-23 03:01:03 -0400892 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100893#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
894 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
895#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000896 NetSendPacket(NetTxPacket, pktlen);
897}
898
899/*
900 * Handle DHCP received packets.
901 */
902static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000903DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
904 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000905{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000906 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000907
Robin Getz0ebf04c2009-07-23 03:01:03 -0400908 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000909 src, dest, len, dhcp_state);
910
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000911 /* Filter out pkts we don't want */
912 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000913 return;
914
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000915 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
916 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000917
918 switch (dhcp_state) {
919 case SELECTING:
920 /*
921 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000922 * to arrive. Then select one, and generate DHCPREQUEST
923 * response. If filename is in format we recognize, assume it
924 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000925 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400926 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200927#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000928 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200929 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000930 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200931#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000932
Robin Getz0ebf04c2009-07-23 03:01:03 -0400933 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000934 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000935
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000936 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
937 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100938 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000939
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200940 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000941 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200942#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000943 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200944#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000945
946 return;
947 break;
948 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400949 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000950
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000951 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
952 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
953 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100954 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000955 /* Store net params from reply */
956 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000957 dhcp_state = BOUND;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000958 printf("DHCP client bound to address %pI4\n",
959 &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000960 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000961 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000962
Simon Glasse4a3d572011-10-27 06:24:32 +0000963 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000964 return;
965 }
966 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200967 case BOUND:
968 /* DHCP client bound to address */
969 break;
wdenk3861aa52002-09-27 23:19:37 +0000970 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000971 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000972 break;
973 }
974
975}
976
977void DhcpRequest(void)
978{
979 BootpRequest();
980}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100981#endif /* CONFIG_CMD_DHCP */