blob: ccf9ee66758492c112595ee7d93c1066eea8b4a0 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk232c1502004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
12#include <command.h>
13#include <net.h>
14#include "bootp.h"
15#include "tftp.h"
wdenk232c1502004-03-12 00:14:09 +000016#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000017#ifdef CONFIG_STATUS_LED
18#include <status_led.h>
19#endif
Kim Phillipsdb7720b2012-07-05 13:19:32 +000020#ifdef CONFIG_BOOTP_RANDOM_DELAY
21#include "net_rand.h"
22#endif
wdenk3861aa52002-09-27 23:19:37 +000023
Joe Hershberger3090b7e32012-05-15 08:59:06 +000024#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000025
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020026#define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
wdenk232c1502004-03-12 00:14:09 +000027#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000028# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000029#else
wdenk232c1502004-03-12 00:14:09 +000030# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000031#endif
32
Joe Hershberger3090b7e32012-05-15 08:59:06 +000033#define PORT_BOOTPS 67 /* BOOTP server UDP port */
34#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000035
Joe Hershberger3090b7e32012-05-15 08:59:06 +000036#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000037#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000038#endif
39
40ulong BootpID;
41int BootpTry;
wdenk3861aa52002-09-27 23:19:37 +000042
Jon Loeliger643d1ab2007-07-09 17:45:14 -050043#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +000044dhcp_state_t dhcp_state = INIT;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000045unsigned long dhcp_leasetime;
46IPaddr_t NetDHCPServerIP;
Luca Ceresoli03eb1292011-04-18 06:19:50 +000047static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
48 unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000049
50/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000051#if 0
52static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000053{
54 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000055 case 1: return "DHCPDISCOVER"; break;
56 case 2: return "DHCPOFFER"; break;
57 case 3: return "DHCPREQUEST"; break;
58 case 4: return "DHCPDECLINE"; break;
59 case 5: return "DHCPACK"; break;
60 case 6: return "DHCPNACK"; break;
61 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000062 default: return "UNKNOWN/INVALID MSG TYPE"; break;
63 }
64}
wdenk3e386912003-04-05 00:53:31 +000065#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050066#endif
wdenk3861aa52002-09-27 23:19:37 +000067
68static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
69{
Joe Hershberger3090b7e32012-05-15 08:59:06 +000070 struct Bootp_t *bp = (struct Bootp_t *) pkt;
wdenk3861aa52002-09-27 23:19:37 +000071 int retval = 0;
72
73 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
74 retval = -1;
Joe Hershbergerf8315732012-05-23 07:58:14 +000075 else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +000076 retval = -2;
77 else if (bp->bp_op != OP_BOOTREQUEST &&
Joe Hershberger3090b7e32012-05-15 08:59:06 +000078 bp->bp_op != OP_BOOTREPLY &&
79 bp->bp_op != DHCP_OFFER &&
80 bp->bp_op != DHCP_ACK &&
81 bp->bp_op != DHCP_NAK)
wdenk3861aa52002-09-27 23:19:37 +000082 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +000083 else if (bp->bp_htype != HWT_ETHER)
84 retval = -4;
85 else if (bp->bp_hlen != HWL_ETHER)
86 retval = -5;
Joe Hershberger3090b7e32012-05-15 08:59:06 +000087 else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
wdenk3861aa52002-09-27 23:19:37 +000088 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +000089
Robin Getz0ebf04c2009-07-23 03:01:03 -040090 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +000091
92 return retval;
93}
94
95/*
96 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
97 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +000098static void BootpCopyNetParams(struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +000099{
Wilson Callan5d110f02007-07-28 10:56:13 -0400100#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000101 IPaddr_t tmp_ip;
102
wdenk3d3befa2004-03-14 15:06:13 +0000103 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
104 if (tmp_ip != 0)
105 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Joe Hershbergercb487f52012-05-23 07:58:06 +0000106 memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400107#endif
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000108 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
wdenk3d3befa2004-03-14 15:06:13 +0000109 if (strlen(bp->bp_file) > 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000110 copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000111
Robin Getz0ebf04c2009-07-23 03:01:03 -0400112 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000113
114 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000115 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000116 * not contain a new value
117 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000118 if (*BootFile)
119 setenv("bootfile", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000120}
121
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000122static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000123{
124 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000125 printf("*** WARNING: %s is too long (%d - max: %d)"
126 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000127 curlen = maxlen - 1;
128 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000129 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000130}
131
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500132#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000133
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000134static void BootpVendorFieldProcess(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000135{
wdenk232c1502004-03-12 00:14:09 +0000136 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000137
Robin Getz0ebf04c2009-07-23 03:01:03 -0400138 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000139 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000140
wdenk232c1502004-03-12 00:14:09 +0000141 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000142
wdenk232c1502004-03-12 00:14:09 +0000143 switch (*ext) {
144 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000145 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000146 if (NetOurSubnetMask == 0)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000147 NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000148 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000149 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000150 break;
wdenk232c1502004-03-12 00:14:09 +0000151 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000152 case 3: /* Gateways list */
153 if (NetOurGatewayIP == 0)
154 NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000155 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000156 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000157 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000158 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000159 break;
160 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000161 if (NetOurDNSIP == 0)
162 NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500163#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000164 if ((NetOurDNS2IP == 0) && (size > 4))
165 NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000166#endif
wdenk3861aa52002-09-27 23:19:37 +0000167 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000168 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000169 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000170 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000171 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000172 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000173 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000174 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000175 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000176 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000177 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000178 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000179 if (NetOurHostName[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000180 size = truncate_sz("Host Name",
181 sizeof(NetOurHostName), size);
182 memcpy(&NetOurHostName, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000183 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000184 }
185 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000186 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000187 if (size == 2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000188 NetBootFileSize = ntohs(*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000189 else if (size == 4)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000190 NetBootFileSize = ntohl(*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000192 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000193 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000194 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000195 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000196 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000197 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000198 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000199 if (NetOurRootPath[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000200 size = truncate_sz("Root Path",
201 sizeof(NetOurRootPath), size);
202 memcpy(&NetOurRootPath, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000203 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000204 }
205 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000206 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000207 /*
wdenk8bde7f72003-06-27 21:31:46 +0000208 * This can be used to send the information of the
209 * vendor area in another file that the client can
210 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000211 */
212 break;
wdenk232c1502004-03-12 00:14:09 +0000213 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000214 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000215 if (NetOurNISDomain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000216 size = truncate_sz("NIS Domain Name",
217 sizeof(NetOurNISDomain), size);
218 memcpy(&NetOurNISDomain, ext + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000219 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000220 }
221 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000222#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
223 case 42: /* NTP server IP */
224 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
225 break;
226#endif
wdenk232c1502004-03-12 00:14:09 +0000227 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000228 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000229 /*
wdenk8bde7f72003-06-27 21:31:46 +0000230 * Binary information to exchange specific
231 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000232 */
233 break;
wdenk232c1502004-03-12 00:14:09 +0000234 /* Reserved (custom) fields (128..254) */
235 }
wdenk3861aa52002-09-27 23:19:37 +0000236}
237
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000238static void BootpVendorProcess(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000239{
wdenk232c1502004-03-12 00:14:09 +0000240 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000241
Robin Getz0ebf04c2009-07-23 03:01:03 -0400242 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000243
wdenk232c1502004-03-12 00:14:09 +0000244 while ((ext < end) && (*ext != 0xff)) {
245 if (*ext == 0) {
246 ext++;
247 } else {
248 u8 *opt = ext;
249
250 ext += ext[1] + 2;
251 if (ext <= end)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000252 BootpVendorFieldProcess(opt);
wdenk232c1502004-03-12 00:14:09 +0000253 }
wdenk3861aa52002-09-27 23:19:37 +0000254 }
wdenk3861aa52002-09-27 23:19:37 +0000255
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000256 debug("[BOOTP] Received fields:\n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500257 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400258 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000259
Mike Frysingerb6446b62009-02-17 00:00:53 -0500260 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400261 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000262
Robin Getz0ebf04c2009-07-23 03:01:03 -0400263 if (NetBootFileSize)
264 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000265
Robin Getz0ebf04c2009-07-23 03:01:03 -0400266 if (NetOurHostName[0])
267 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000268
Robin Getz0ebf04c2009-07-23 03:01:03 -0400269 if (NetOurRootPath[0])
270 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000271
Robin Getz0ebf04c2009-07-23 03:01:03 -0400272 if (NetOurNISDomain[0])
273 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000274
Robin Getz0ebf04c2009-07-23 03:01:03 -0400275 if (NetBootFileSize)
276 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000277
278#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
279 if (NetNtpServerIP)
280 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
281#endif
wdenk3861aa52002-09-27 23:19:37 +0000282}
Simon Glass09349862011-06-13 16:13:12 -0700283
wdenk3861aa52002-09-27 23:19:37 +0000284/*
285 * Handle a BOOTP received packet.
286 */
287static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000288BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
289 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000290{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000291 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000292
Robin Getz0ebf04c2009-07-23 03:01:03 -0400293 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000294 src, dest, len, sizeof(struct Bootp_t));
wdenk3861aa52002-09-27 23:19:37 +0000295
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000296 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000297
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000298 /* Filter out pkts we don't want */
299 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000300 return;
301
302 /*
wdenk232c1502004-03-12 00:14:09 +0000303 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000304 */
305#ifdef CONFIG_STATUS_LED
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000306 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000307#endif
308
309 BootpCopyNetParams(bp); /* Store net parameters from reply */
310
311 /* Retrieve extended information (we must parse the vendor area) */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000312 if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200313 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000314
315 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000316 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000317
Robin Getz0ebf04c2009-07-23 03:01:03 -0400318 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000319
Simon Glasse4a3d572011-10-27 06:24:32 +0000320 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000321}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500322#endif
wdenk3861aa52002-09-27 23:19:37 +0000323
324/*
325 * Timeout on BOOTP/DHCP request.
326 */
327static void
328BootpTimeout(void)
329{
330 if (BootpTry >= TIMEOUT_COUNT) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000331#ifdef CONFIG_BOOTP_MAY_FAIL
332 puts("\nRetry count exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000333 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000334#else
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000335 puts("\nRetry count exceeded; starting again\n");
336 NetStartAgain();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000337#endif
wdenk3861aa52002-09-27 23:19:37 +0000338 } else {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000339 NetSetTimeout(TIMEOUT, BootpTimeout);
340 BootpRequest();
wdenk3861aa52002-09-27 23:19:37 +0000341 }
342}
343
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000344#define put_vci(e, str) \
345 do { \
346 size_t vci_strlen = strlen(str); \
347 *e++ = 60; /* Vendor Class Identifier */ \
348 *e++ = vci_strlen; \
349 memcpy(e, str, vci_strlen); \
350 e += vci_strlen; \
351 } while (0)
352
wdenk3861aa52002-09-27 23:19:37 +0000353/*
354 * Initialize BOOTP extension fields in the request.
355 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500356#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000357static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
358 IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000359{
wdenk232c1502004-03-12 00:14:09 +0000360 u8 *start = e;
361 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000362#if defined(CONFIG_BOOTP_PXE)
363 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000364 u16 clientarch;
365#endif
wdenk232c1502004-03-12 00:14:09 +0000366
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500367#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000368 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000369#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500370#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200371 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000372#endif
wdenk3861aa52002-09-27 23:19:37 +0000373
wdenk232c1502004-03-12 00:14:09 +0000374 *e++ = 99; /* RFC1048 Magic Cookie */
375 *e++ = 130;
376 *e++ = 83;
377 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000378
wdenk232c1502004-03-12 00:14:09 +0000379 *e++ = 53; /* DHCP Message Type */
380 *e++ = 1;
381 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000382
wdenk232c1502004-03-12 00:14:09 +0000383 *e++ = 57; /* Maximum DHCP Message Size */
384 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000385 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
386 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000387
wdenk232c1502004-03-12 00:14:09 +0000388 if (ServerID) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000389 int tmp = ntohl(ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000390
wdenk232c1502004-03-12 00:14:09 +0000391 *e++ = 54; /* ServerID */
392 *e++ = 4;
393 *e++ = tmp >> 24;
394 *e++ = tmp >> 16;
395 *e++ = tmp >> 8;
396 *e++ = tmp & 0xff;
397 }
wdenk3861aa52002-09-27 23:19:37 +0000398
wdenk232c1502004-03-12 00:14:09 +0000399 if (RequestedIP) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000400 int tmp = ntohl(RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000401
wdenk232c1502004-03-12 00:14:09 +0000402 *e++ = 50; /* Requested IP */
403 *e++ = 4;
404 *e++ = tmp >> 24;
405 *e++ = tmp >> 16;
406 *e++ = tmp >> 8;
407 *e++ = tmp & 0xff;
408 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500409#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000410 hostname = getenv("hostname");
411 if (hostname) {
412 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000413
414 *e++ = 12; /* Hostname */
415 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000416 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000417 e += hostnamelen;
418 }
stroesefe389a82003-08-28 14:17:32 +0000419#endif
420
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000421#if defined(CONFIG_BOOTP_PXE)
422 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
423 *e++ = 93; /* Client System Architecture */
424 *e++ = 2;
425 *e++ = (clientarch >> 8) & 0xff;
426 *e++ = clientarch & 0xff;
427
428 *e++ = 94; /* Client Network Interface Identifier */
429 *e++ = 3;
430 *e++ = 1; /* type field for UNDI */
431 *e++ = 0; /* major revision */
432 *e++ = 0; /* minor revision */
433
434 uuid = getenv("pxeuuid");
435
436 if (uuid) {
437 if (uuid_str_valid(uuid)) {
438 *e++ = 97; /* Client Machine Identifier */
439 *e++ = 17;
440 *e++ = 0; /* type 0 - UUID */
441
442 uuid_str_to_bin(uuid, e);
443 e += 16;
444 } else {
445 printf("Invalid pxeuuid: %s\n", uuid);
446 }
447 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000448#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000449
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000450#ifdef CONFIG_BOOTP_VCI_STRING
451 put_vci(e, CONFIG_BOOTP_VCI_STRING);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000452#endif
453
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500454#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000455 x = dhcp_vendorex_prep(e);
456 if (x)
wdenk232c1502004-03-12 00:14:09 +0000457 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000458#endif
459
wdenk232c1502004-03-12 00:14:09 +0000460 *e++ = 55; /* Parameter Request List */
461 cnt = e++; /* Pointer to count of requested items */
462 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500463#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000464 *e++ = 1; /* Subnet Mask */
465 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000466#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500467#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000468 *e++ = 2;
469 *cnt += 1;
470#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500471#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000472 *e++ = 3; /* Router Option */
473 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000474#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500475#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000476 *e++ = 6; /* DNS Server(s) */
477 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000478#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500479#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000480 *e++ = 12; /* Hostname */
481 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000482#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500483#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000484 *e++ = 13; /* Boot File Size */
485 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000486#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500487#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000488 *e++ = 17; /* Boot path */
489 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000490#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500491#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000492 *e++ = 40; /* NIS Domain name request */
493 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000494#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500495#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000496 *e++ = 42;
497 *cnt += 1;
498#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800499 /* no options, so back up to avoid sending an empty request list */
500 if (*cnt == 0)
501 e -= 2;
502
wdenk232c1502004-03-12 00:14:09 +0000503 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000504
wdenk232c1502004-03-12 00:14:09 +0000505 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000506#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800507 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000508 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000509#endif
510
wdenk232c1502004-03-12 00:14:09 +0000511 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000512}
513
Jon Loeliger610f2e92007-07-10 11:05:02 -0500514#else
wdenk3861aa52002-09-27 23:19:37 +0000515/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000516 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000517 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000518static int BootpExtended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000519{
wdenk232c1502004-03-12 00:14:09 +0000520 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000521
wdenk232c1502004-03-12 00:14:09 +0000522 *e++ = 99; /* RFC1048 Magic Cookie */
523 *e++ = 130;
524 *e++ = 83;
525 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000526
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500527#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000528 *e++ = 53; /* DHCP Message Type */
529 *e++ = 1;
530 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000531
wdenk232c1502004-03-12 00:14:09 +0000532 *e++ = 57; /* Maximum DHCP Message Size */
533 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000534 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
535 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500536#endif
wdenk3861aa52002-09-27 23:19:37 +0000537
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000538#ifdef CONFIG_BOOTP_VCI_STRING
539 put_vci(e, CONFIG_BOOTP_VCI_STRING);
540#endif
541
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500542#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000543 *e++ = 1; /* Subnet mask request */
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_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000549 *e++ = 3; /* Default gateway request */
550 *e++ = 4;
551 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000552#endif
553
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500554#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000555 *e++ = 6; /* Domain Name Server */
556 *e++ = 4;
557 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000558#endif
559
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500560#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000561 *e++ = 12; /* Host name request */
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_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000567 *e++ = 13; /* Boot file size */
568 *e++ = 2;
569 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000570#endif
571
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500572#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000573 *e++ = 17; /* Boot path */
574 *e++ = 32;
575 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000576#endif
577
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500578#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000579 *e++ = 40; /* NIS Domain name request */
580 *e++ = 32;
581 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000582#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000583#if defined(CONFIG_BOOTP_NTPSERVER)
584 *e++ = 42;
585 *e++ = 4;
586 e += 4;
587#endif
wdenk3861aa52002-09-27 23:19:37 +0000588
wdenk232c1502004-03-12 00:14:09 +0000589 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000590
wdenk232c1502004-03-12 00:14:09 +0000591 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000592}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500593#endif
wdenk3861aa52002-09-27 23:19:37 +0000594
595void
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000596BootpRequest(void)
wdenk3861aa52002-09-27 23:19:37 +0000597{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000598 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000599 struct Bootp_t *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000600 int extlen, pktlen, iplen;
601 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000602#ifdef CONFIG_BOOTP_RANDOM_DELAY
603 ulong i, rand_ms;
604#endif
wdenk3861aa52002-09-27 23:19:37 +0000605
Simon Glass573f14f2011-12-10 11:08:06 +0000606 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500607#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000608 dhcp_state = INIT;
609#endif
610
611#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000612 if (BootpTry == 0)
613 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000614
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000615 if (BootpTry <= 2) /* Start with max 1024 * 1ms */
616 rand_ms = rand() >> (22 - BootpTry);
617 else /* After 3rd BOOTP request max 8192 * 1ms */
618 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000619
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000620 printf("Random delay: %ld ms...\n", rand_ms);
621 for (i = 0; i < rand_ms; i++)
wdenk3861aa52002-09-27 23:19:37 +0000622 udelay(1000); /*Wait 1ms*/
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000623
wdenk3861aa52002-09-27 23:19:37 +0000624#endif /* CONFIG_BOOTP_RANDOM_DELAY */
625
626 printf("BOOTP broadcast %d\n", ++BootpTry);
627 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000628 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000629
Joe Hershbergerae446f52012-05-23 07:59:10 +0000630 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
631 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000632
633 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000634 * Next line results in incorrect packet size being transmitted,
635 * resulting in errors in some DHCP servers, reporting missing bytes.
636 * Size must be set in packet header after extension length has been
637 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000638 * C. Hallinan, DS4.COM, Inc.
639 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000640 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000641 sizeof (struct Bootp_t)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000642 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000643 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000644
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000645 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000646 bp->bp_op = OP_BOOTREQUEST;
647 bp->bp_htype = HWT_ETHER;
648 bp->bp_hlen = HWL_ETHER;
649 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200650 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000651 NetWriteIP(&bp->bp_ciaddr, 0);
652 NetWriteIP(&bp->bp_yiaddr, 0);
653 NetWriteIP(&bp->bp_siaddr, 0);
654 NetWriteIP(&bp->bp_giaddr, 0);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000655 memcpy(bp->bp_chaddr, NetOurEther, 6);
656 copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000657
658 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500659#if defined(CONFIG_CMD_DHCP)
Joe Hershbergerae446f52012-05-23 07:59:10 +0000660 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000661#else
Joe Hershbergerae446f52012-05-23 07:59:10 +0000662 extlen = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500663#endif
wdenk3861aa52002-09-27 23:19:37 +0000664
665 /*
666 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200667 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000668 */
669 BootpID = ((ulong)NetOurEther[2] << 24)
670 | ((ulong)NetOurEther[3] << 16)
671 | ((ulong)NetOurEther[4] << 8)
672 | (ulong)NetOurEther[5];
673 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000674 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000675 NetCopyLong(&bp->bp_id, &BootpID);
676
677 /*
678 * Calculate proper packet lengths taking into account the
679 * variable size of the options field
680 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000681 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
682 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000683 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200684 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000685
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500686#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000687 dhcp_state = SELECTING;
Joe Hershbergerece223b2012-05-23 07:59:15 +0000688 net_set_udp_handler(DhcpHandler);
wdenk3861aa52002-09-27 23:19:37 +0000689#else
Joe Hershbergerece223b2012-05-23 07:59:15 +0000690 net_set_udp_handler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500691#endif
wdenk3861aa52002-09-27 23:19:37 +0000692 NetSendPacket(NetTxPacket, pktlen);
693}
694
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500695#if defined(CONFIG_CMD_DHCP)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000696static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000697{
wdenk3e386912003-04-05 00:53:31 +0000698 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000699 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200700#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
701 int *to_ptr;
702#endif
wdenk3861aa52002-09-27 23:19:37 +0000703
wdenk232c1502004-03-12 00:14:09 +0000704 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000705 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000706 switch (*popt) {
707 case 1:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000708 NetCopyIP(&NetOurSubnetMask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000709 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500710#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000711 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200712 to_ptr = &NetTimeOffset;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000713 NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
714 NetTimeOffset = ntohl(NetTimeOffset);
wdenkea287de2005-04-01 00:25:43 +0000715 break;
716#endif
wdenk232c1502004-03-12 00:14:09 +0000717 case 3:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000718 NetCopyIP(&NetOurGatewayIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000719 break;
720 case 6:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000721 NetCopyIP(&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500722#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000723 if (*(popt + 1) > 4)
724 NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000725#endif
wdenk232c1502004-03-12 00:14:09 +0000726 break;
727 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000728 size = truncate_sz("Host Name",
729 sizeof(NetOurHostName), oplen);
730 memcpy(&NetOurHostName, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000731 NetOurHostName[size] = 0;
732 break;
733 case 15: /* Ignore Domain Name Option */
734 break;
735 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000736 size = truncate_sz("Root Path",
737 sizeof(NetOurRootPath), oplen);
738 memcpy(&NetOurRootPath, popt + 2, size);
wdenk232c1502004-03-12 00:14:09 +0000739 NetOurRootPath[size] = 0;
740 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000741 case 28: /* Ignore Broadcast Address Option */
742 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500743#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000744 case 42: /* NTP server IP */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000745 NetCopyIP(&NetNtpServerIP, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000746 break;
747#endif
wdenk232c1502004-03-12 00:14:09 +0000748 case 51:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000749 NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000750 break;
751 case 53: /* Ignore Message Type Option */
752 break;
753 case 54:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000754 NetCopyIP(&NetDHCPServerIP, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000755 break;
756 case 58: /* Ignore Renewal Time Option */
757 break;
758 case 59: /* Ignore Rebinding Time Option */
759 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100760 case 66: /* Ignore TFTP server name */
761 break;
762 case 67: /* vendor opt bootfile */
763 /*
764 * I can't use dhcp_vendorex_proc here because I need
765 * to write into the bootp packet - even then I had to
766 * pass the bootp packet pointer into here as the
767 * second arg
768 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000769 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100770 sizeof(bp->bp_file),
771 oplen);
772 if (bp->bp_file[0] == '\0' && size > 0) {
773 /*
774 * only use vendor boot file if we didn't
775 * receive a boot file in the main non-vendor
776 * part of the packet - god only knows why
777 * some vendors chose not to use this perfectly
778 * good spot to store the boot file (join on
779 * Tru64 Unix) it seems mind bogglingly crazy
780 * to me
781 */
782 printf("*** WARNING: using vendor "
783 "optional boot file\n");
784 memcpy(bp->bp_file, popt + 2, size);
785 bp->bp_file[size] = '\0';
786 }
787 break;
wdenk232c1502004-03-12 00:14:09 +0000788 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500789#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000790 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000791 break;
wdenk3861aa52002-09-27 23:19:37 +0000792#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000793 printf("*** Unhandled DHCP Option in OFFER/ACK:"
794 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000795 break;
wdenk3861aa52002-09-27 23:19:37 +0000796 }
797 popt += oplen + 2; /* Process next option */
798 }
799}
800
801static int DhcpMessageType(unsigned char *popt)
802{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000803 if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000804 return -1;
805
806 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000807 while (*popt != 0xff) {
808 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000809 return *(popt + 2);
810 popt += *(popt + 1) + 2; /* Scan through all options */
811 }
812 return -1;
813}
814
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000815static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000816{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000817 uchar *pkt, *iphdr;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000818 struct Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000819 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000820 int eth_hdr_size;
wdenk47cd00f2003-03-06 13:39:27 +0000821 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000822
Robin Getz0ebf04c2009-07-23 03:01:03 -0400823 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000824 pkt = NetTxPacket;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000825 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000826
Joe Hershbergerae446f52012-05-23 07:59:10 +0000827 eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP);
828 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000829
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000830 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000831 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000832
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000833 bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000834 bp->bp_op = OP_BOOTREQUEST;
835 bp->bp_htype = HWT_ETHER;
836 bp->bp_hlen = HWL_ETHER;
837 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200838 bp->bp_secs = htons(get_timer(0) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000839 /* Do not set the client IP, your IP, or server IP yet, since it
840 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400841
Wolfgang Denkc6686702006-10-12 00:01:08 +0200842 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200843 * RFC3046 requires Relay Agents to discard packets with
844 * nonzero and offered giaddr
845 */
846 NetWriteIP(&bp->bp_giaddr, 0);
847
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000848 memcpy(bp->bp_chaddr, NetOurEther, 6);
wdenk3861aa52002-09-27 23:19:37 +0000849
850 /*
851 * ID is the id of the OFFER packet
852 */
853
854 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
855
856 /*
857 * Copy options from OFFER packet if present
858 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400859
860 /* Copy offered IP into the parameters request list */
861 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000862 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
863 NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000864
Joe Hershbergerae446f52012-05-23 07:59:10 +0000865 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
866 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger4b11c912012-05-23 07:59:07 +0000867 net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000868
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100869#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
870 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
871#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000872 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000873 NetSendPacket(NetTxPacket, pktlen);
874}
875
876/*
877 * Handle DHCP received packets.
878 */
879static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000880DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
881 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000882{
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000883 struct Bootp_t *bp = (struct Bootp_t *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000884
Robin Getz0ebf04c2009-07-23 03:01:03 -0400885 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000886 src, dest, len, dhcp_state);
887
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000888 /* Filter out pkts we don't want */
889 if (BootpCheckPkt(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000890 return;
891
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000892 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
893 " %d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000894
895 switch (dhcp_state) {
896 case SELECTING:
897 /*
898 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000899 * to arrive. Then select one, and generate DHCPREQUEST
900 * response. If filename is in format we recognize, assume it
901 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000902 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400903 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200904#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000905 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200906 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000907 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200908#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000909
Robin Getz0ebf04c2009-07-23 03:01:03 -0400910 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000911 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000912
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000913 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
914 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100915 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000916
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200917 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000918 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200919#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000920 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200921#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000922
923 return;
924 break;
925 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400926 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000927
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000928 if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
929 if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
930 htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100931 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000932 /* Store net params from reply */
933 BootpCopyNetParams(bp);
wdenk3861aa52002-09-27 23:19:37 +0000934 dhcp_state = BOUND;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000935 printf("DHCP client bound to address %pI4\n",
936 &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000937 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000938 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000939
Simon Glasse4a3d572011-10-27 06:24:32 +0000940 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000941 return;
942 }
943 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200944 case BOUND:
945 /* DHCP client bound to address */
946 break;
wdenk3861aa52002-09-27 23:19:37 +0000947 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000948 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000949 break;
950 }
951
952}
953
954void DhcpRequest(void)
955{
956 BootpRequest();
957}
Wolfgang Denk992742a2007-11-03 23:09:27 +0100958#endif /* CONFIG_CMD_DHCP */