blob: b1b7b0c1ef4023324ebafe075a85f62972fbee1b [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
wdenk232c1502004-03-12 00:14:09 +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
wdenk3861aa52002-09-27 23:19:37 +000026# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
27#else
wdenk232c1502004-03-12 00:14:09 +000028# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000029#endif
30
31#define PORT_BOOTPS 67 /* BOOTP server UDP port */
32#define PORT_BOOTPC 68 /* BOOTP client UDP port */
33
34#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;
wdenk682011f2003-06-03 23:54:09 +000046unsigned long dhcp_leasetime = 0;
stroese759a51b2003-04-10 13:26:44 +000047IPaddr_t NetDHCPServerIP = 0;
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
wdenk3861aa52002-09-27 23:19:37 +000067
Jon Loeliger1fe80d72007-07-09 22:08:34 -050068#if defined(CONFIG_BOOTP_VENDOREX)
wdenk3861aa52002-09-27 23:19:37 +000069extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
70extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */
John Rigby2c872ca2012-08-09 14:17:11 -060071u8 *__dhcp_vendorex_prep(u8 *e)
72{
73 char *ptr;
wdenk3861aa52002-09-27 23:19:37 +000074
John Rigby2c872ca2012-08-09 14:17:11 -060075 /* DHCP vendor-class-identifier = 60 */
76 if ((ptr = getenv("dhcp_vendor-class-identifier"))) {
77 *e++ = 60;
78 *e++ = strlen(ptr);
79 while (*ptr)
80 *e++ = *ptr++;
81 }
82 /* DHCP_CLIENT_IDENTIFIER = 61 */
83 if ((ptr = getenv("dhcp_client_id"))) {
84 *e++ = 61;
85 *e++ = strlen(ptr);
86 while (*ptr)
87 *e++ = *ptr++;
88 }
89
90 return e;
91}
92
93u8 *__dhcp_vendorex_proc(u8 *popt)
94{
95 return NULL;
96}
97u8 *dhcp_vendorex_prep(u8 *e) __attribute__((weak, alias("__dhcp_vendorex_prep")));
98u8 *dhcp_vendorex_proc(u8 *e) __attribute__((weak, alias("__dhcp_vendorex_proc")));
99#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -0500100#endif
wdenk3861aa52002-09-27 23:19:37 +0000101
102static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
103{
104 Bootp_t *bp = (Bootp_t *) pkt;
105 int retval = 0;
106
107 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
108 retval = -1;
109 else if (len < sizeof (Bootp_t) - OPT_SIZE)
110 retval = -2;
111 else if (bp->bp_op != OP_BOOTREQUEST &&
112 bp->bp_op != OP_BOOTREPLY &&
113 bp->bp_op != DHCP_OFFER &&
114 bp->bp_op != DHCP_ACK &&
115 bp->bp_op != DHCP_NAK ) {
116 retval = -3;
117 }
118 else if (bp->bp_htype != HWT_ETHER)
119 retval = -4;
120 else if (bp->bp_hlen != HWL_ETHER)
121 retval = -5;
122 else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) {
123 retval = -6;
124 }
125
Robin Getz0ebf04c2009-07-23 03:01:03 -0400126 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000127
128 return retval;
129}
130
131/*
132 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
133 */
wdenk3e386912003-04-05 00:53:31 +0000134static void BootpCopyNetParams(Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000135{
Anatolij Gustschinc0fe04b2011-11-19 10:29:58 +0000136 __maybe_unused IPaddr_t tmp_ip;
wdenk3d3befa2004-03-14 15:06:13 +0000137
wdenk3861aa52002-09-27 23:19:37 +0000138 NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
Wilson Callan5d110f02007-07-28 10:56:13 -0400139#if !defined(CONFIG_BOOTP_SERVERIP)
wdenk3d3befa2004-03-14 15:06:13 +0000140 NetCopyIP(&tmp_ip, &bp->bp_siaddr);
141 if (tmp_ip != 0)
142 NetCopyIP(&NetServerIP, &bp->bp_siaddr);
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400143 memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
Wilson Callan5d110f02007-07-28 10:56:13 -0400144#endif
wdenk3d3befa2004-03-14 15:06:13 +0000145 if (strlen(bp->bp_file) > 0)
146 copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
wdenk3861aa52002-09-27 23:19:37 +0000147
Robin Getz0ebf04c2009-07-23 03:01:03 -0400148 debug("Bootfile: %s\n", BootFile);
wdenk3861aa52002-09-27 23:19:37 +0000149
150 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000151 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000152 * not contain a new value
153 */
154 if (*BootFile) {
155 setenv ("bootfile", BootFile);
156 }
157}
158
159static int truncate_sz (const char *name, int maxlen, int curlen)
160{
161 if (curlen >= maxlen) {
162 printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n",
163 name, curlen, maxlen);
164 curlen = maxlen - 1;
165 }
166 return (curlen);
167}
168
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500169#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000170
wdenk232c1502004-03-12 00:14:09 +0000171static void BootpVendorFieldProcess (u8 * ext)
wdenk3861aa52002-09-27 23:19:37 +0000172{
wdenk232c1502004-03-12 00:14:09 +0000173 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000174
Robin Getz0ebf04c2009-07-23 03:01:03 -0400175 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
wdenk232c1502004-03-12 00:14:09 +0000176 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000177
wdenk232c1502004-03-12 00:14:09 +0000178 NetBootFileSize = 0;
wdenk3861aa52002-09-27 23:19:37 +0000179
wdenk232c1502004-03-12 00:14:09 +0000180 switch (*ext) {
181 /* Fixed length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700182 case 1: /* Subnet mask */
wdenk3861aa52002-09-27 23:19:37 +0000183 if (NetOurSubnetMask == 0)
wdenk232c1502004-03-12 00:14:09 +0000184 NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000185 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700186 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000187 break;
wdenk232c1502004-03-12 00:14:09 +0000188 /* Variable length fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700189 case 3: /* Gateways list */
wdenk3861aa52002-09-27 23:19:37 +0000190 if (NetOurGatewayIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000191 NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000192 }
193 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700194 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000195 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700196 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000197 break;
198 case 6:
199 if (NetOurDNSIP == 0) {
wdenk232c1502004-03-12 00:14:09 +0000200 NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000201 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500202#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000203 if ((NetOurDNS2IP == 0) && (size > 4)) {
wdenk232c1502004-03-12 00:14:09 +0000204 NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000205 }
206#endif
wdenk3861aa52002-09-27 23:19:37 +0000207 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700208 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000209 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700210 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000211 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700212 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000213 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700214 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000215 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700216 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000217 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700218 case 12: /* Host name */
wdenk3861aa52002-09-27 23:19:37 +0000219 if (NetOurHostName[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000220 size = truncate_sz ("Host Name", sizeof (NetOurHostName), size);
221 memcpy (&NetOurHostName, ext + 2, size);
222 NetOurHostName[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000223 }
224 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700225 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000226 if (size == 2)
wdenk232c1502004-03-12 00:14:09 +0000227 NetBootFileSize = ntohs (*(ushort *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000228 else if (size == 4)
wdenk232c1502004-03-12 00:14:09 +0000229 NetBootFileSize = ntohl (*(ulong *) (ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000230 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700231 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000232 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700233 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000234 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700235 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000236 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700237 case 17: /* Root path */
wdenk3861aa52002-09-27 23:19:37 +0000238 if (NetOurRootPath[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000239 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size);
240 memcpy (&NetOurRootPath, ext + 2, size);
241 NetOurRootPath[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000242 }
243 break;
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700244 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000245 /*
wdenk8bde7f72003-06-27 21:31:46 +0000246 * This can be used to send the information of the
247 * vendor area in another file that the client can
248 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000249 */
250 break;
wdenk232c1502004-03-12 00:14:09 +0000251 /* IP host layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700252 case 40: /* NIS Domain name */
wdenk3861aa52002-09-27 23:19:37 +0000253 if (NetOurNISDomain[0] == 0) {
wdenk232c1502004-03-12 00:14:09 +0000254 size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size);
255 memcpy (&NetOurNISDomain, ext + 2, size);
256 NetOurNISDomain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000257 }
258 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000259#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
260 case 42: /* NTP server IP */
261 NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
262 break;
263#endif
wdenk232c1502004-03-12 00:14:09 +0000264 /* Application layer fields */
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700265 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000266 /*
wdenk8bde7f72003-06-27 21:31:46 +0000267 * Binary information to exchange specific
268 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000269 */
270 break;
wdenk232c1502004-03-12 00:14:09 +0000271 /* Reserved (custom) fields (128..254) */
272 }
wdenk3861aa52002-09-27 23:19:37 +0000273}
274
wdenk232c1502004-03-12 00:14:09 +0000275static void BootpVendorProcess (u8 * ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000276{
wdenk232c1502004-03-12 00:14:09 +0000277 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000278
Robin Getz0ebf04c2009-07-23 03:01:03 -0400279 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000280
wdenk232c1502004-03-12 00:14:09 +0000281 while ((ext < end) && (*ext != 0xff)) {
282 if (*ext == 0) {
283 ext++;
284 } else {
285 u8 *opt = ext;
286
287 ext += ext[1] + 2;
288 if (ext <= end)
289 BootpVendorFieldProcess (opt);
290 }
wdenk3861aa52002-09-27 23:19:37 +0000291 }
wdenk3861aa52002-09-27 23:19:37 +0000292
Robin Getz0ebf04c2009-07-23 03:01:03 -0400293 debug("[BOOTP] Received fields: \n");
Mike Frysingerb6446b62009-02-17 00:00:53 -0500294 if (NetOurSubnetMask)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400295 debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
wdenk3861aa52002-09-27 23:19:37 +0000296
Mike Frysingerb6446b62009-02-17 00:00:53 -0500297 if (NetOurGatewayIP)
Robin Getz0ebf04c2009-07-23 03:01:03 -0400298 debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
wdenk3861aa52002-09-27 23:19:37 +0000299
Robin Getz0ebf04c2009-07-23 03:01:03 -0400300 if (NetBootFileSize)
301 debug("NetBootFileSize : %d\n", NetBootFileSize);
wdenk3861aa52002-09-27 23:19:37 +0000302
Robin Getz0ebf04c2009-07-23 03:01:03 -0400303 if (NetOurHostName[0])
304 debug("NetOurHostName : %s\n", NetOurHostName);
wdenk3861aa52002-09-27 23:19:37 +0000305
Robin Getz0ebf04c2009-07-23 03:01:03 -0400306 if (NetOurRootPath[0])
307 debug("NetOurRootPath : %s\n", NetOurRootPath);
wdenk3861aa52002-09-27 23:19:37 +0000308
Robin Getz0ebf04c2009-07-23 03:01:03 -0400309 if (NetOurNISDomain[0])
310 debug("NetOurNISDomain : %s\n", NetOurNISDomain);
wdenk3861aa52002-09-27 23:19:37 +0000311
Robin Getz0ebf04c2009-07-23 03:01:03 -0400312 if (NetBootFileSize)
313 debug("NetBootFileSize: %d\n", NetBootFileSize);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000314
315#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
316 if (NetNtpServerIP)
317 debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
318#endif
wdenk3861aa52002-09-27 23:19:37 +0000319}
Simon Glass09349862011-06-13 16:13:12 -0700320
wdenk3861aa52002-09-27 23:19:37 +0000321/*
322 * Handle a BOOTP received packet.
323 */
324static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000325BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
326 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000327{
328 Bootp_t *bp;
wdenk3861aa52002-09-27 23:19:37 +0000329
Robin Getz0ebf04c2009-07-23 03:01:03 -0400330 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
wdenk3861aa52002-09-27 23:19:37 +0000331 src, dest, len, sizeof (Bootp_t));
332
333 bp = (Bootp_t *)pkt;
334
wdenk232c1502004-03-12 00:14:09 +0000335 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000336 return;
337
338 /*
wdenk232c1502004-03-12 00:14:09 +0000339 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000340 */
341#ifdef CONFIG_STATUS_LED
342 status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF);
343#endif
344
345 BootpCopyNetParams(bp); /* Store net parameters from reply */
346
347 /* Retrieve extended information (we must parse the vendor area) */
348 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200349 BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000350
351 NetSetTimeout(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000352 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000353
Robin Getz0ebf04c2009-07-23 03:01:03 -0400354 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000355
Simon Glasse4a3d572011-10-27 06:24:32 +0000356 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000357}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500358#endif
wdenk3861aa52002-09-27 23:19:37 +0000359
360/*
361 * Timeout on BOOTP/DHCP request.
362 */
363static void
364BootpTimeout(void)
365{
366 if (BootpTry >= TIMEOUT_COUNT) {
367 puts ("\nRetry count exceeded; starting again\n");
368 NetStartAgain ();
369 } else {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200370 NetSetTimeout (TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000371 BootpRequest ();
372 }
373}
374
375/*
376 * Initialize BOOTP extension fields in the request.
377 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500378#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000379static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
wdenk3861aa52002-09-27 23:19:37 +0000380{
wdenk232c1502004-03-12 00:14:09 +0000381 u8 *start = e;
382 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000383#if defined(CONFIG_BOOTP_PXE)
384 char *uuid;
385 size_t vci_strlen;
386 u16 clientarch;
387#endif
wdenk232c1502004-03-12 00:14:09 +0000388
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500389#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000390 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000391#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500392#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200393 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000394#endif
wdenk3861aa52002-09-27 23:19:37 +0000395
wdenk232c1502004-03-12 00:14:09 +0000396 *e++ = 99; /* RFC1048 Magic Cookie */
397 *e++ = 130;
398 *e++ = 83;
399 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000400
wdenk232c1502004-03-12 00:14:09 +0000401 *e++ = 53; /* DHCP Message Type */
402 *e++ = 1;
403 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000404
wdenk232c1502004-03-12 00:14:09 +0000405 *e++ = 57; /* Maximum DHCP Message Size */
406 *e++ = 2;
407 *e++ = (576 - 312 + OPT_SIZE) >> 8;
408 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000409
wdenk232c1502004-03-12 00:14:09 +0000410 if (ServerID) {
411 int tmp = ntohl (ServerID);
wdenk3861aa52002-09-27 23:19:37 +0000412
wdenk232c1502004-03-12 00:14:09 +0000413 *e++ = 54; /* ServerID */
414 *e++ = 4;
415 *e++ = tmp >> 24;
416 *e++ = tmp >> 16;
417 *e++ = tmp >> 8;
418 *e++ = tmp & 0xff;
419 }
wdenk3861aa52002-09-27 23:19:37 +0000420
wdenk232c1502004-03-12 00:14:09 +0000421 if (RequestedIP) {
422 int tmp = ntohl (RequestedIP);
wdenk3861aa52002-09-27 23:19:37 +0000423
wdenk232c1502004-03-12 00:14:09 +0000424 *e++ = 50; /* Requested IP */
425 *e++ = 4;
426 *e++ = tmp >> 24;
427 *e++ = tmp >> 16;
428 *e++ = tmp >> 8;
429 *e++ = tmp & 0xff;
430 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500431#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000432 if ((hostname = getenv ("hostname"))) {
433 int hostnamelen = strlen (hostname);
434
435 *e++ = 12; /* Hostname */
436 *e++ = hostnamelen;
437 memcpy (e, hostname, hostnamelen);
438 e += hostnamelen;
439 }
stroesefe389a82003-08-28 14:17:32 +0000440#endif
441
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000442#if defined(CONFIG_BOOTP_PXE)
443 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
444 *e++ = 93; /* Client System Architecture */
445 *e++ = 2;
446 *e++ = (clientarch >> 8) & 0xff;
447 *e++ = clientarch & 0xff;
448
449 *e++ = 94; /* Client Network Interface Identifier */
450 *e++ = 3;
451 *e++ = 1; /* type field for UNDI */
452 *e++ = 0; /* major revision */
453 *e++ = 0; /* minor revision */
454
455 uuid = getenv("pxeuuid");
456
457 if (uuid) {
458 if (uuid_str_valid(uuid)) {
459 *e++ = 97; /* Client Machine Identifier */
460 *e++ = 17;
461 *e++ = 0; /* type 0 - UUID */
462
463 uuid_str_to_bin(uuid, e);
464 e += 16;
465 } else {
466 printf("Invalid pxeuuid: %s\n", uuid);
467 }
468 }
469
470 *e++ = 60; /* Vendor Class Identifier */
471 vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
472 *e++ = vci_strlen;
473 memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
474 e += vci_strlen;
475#endif
476
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500477#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000478 if ((x = dhcp_vendorex_prep (e)))
479 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000480#endif
481
wdenk232c1502004-03-12 00:14:09 +0000482 *e++ = 55; /* Parameter Request List */
483 cnt = e++; /* Pointer to count of requested items */
484 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500485#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000486 *e++ = 1; /* Subnet Mask */
487 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000488#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500489#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000490 *e++ = 2;
491 *cnt += 1;
492#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500493#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000494 *e++ = 3; /* Router Option */
495 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000496#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500497#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000498 *e++ = 6; /* DNS Server(s) */
499 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000500#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500501#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000502 *e++ = 12; /* Hostname */
503 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000504#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500505#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000506 *e++ = 13; /* Boot File Size */
507 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000508#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500509#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000510 *e++ = 17; /* Boot path */
511 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000512#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500513#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000514 *e++ = 40; /* NIS Domain name request */
515 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000516#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500517#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000518 *e++ = 42;
519 *cnt += 1;
520#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800521 /* no options, so back up to avoid sending an empty request list */
522 if (*cnt == 0)
523 e -= 2;
524
wdenk232c1502004-03-12 00:14:09 +0000525 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000526
wdenk232c1502004-03-12 00:14:09 +0000527 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000528#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800529 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000530 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000531#endif
532
wdenk232c1502004-03-12 00:14:09 +0000533 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000534}
535
Jon Loeliger610f2e92007-07-10 11:05:02 -0500536#else
wdenk3861aa52002-09-27 23:19:37 +0000537/*
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500538 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000539 */
wdenk232c1502004-03-12 00:14:09 +0000540static int BootpExtended (u8 * e)
wdenk3861aa52002-09-27 23:19:37 +0000541{
wdenk232c1502004-03-12 00:14:09 +0000542 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000543
wdenk232c1502004-03-12 00:14:09 +0000544 *e++ = 99; /* RFC1048 Magic Cookie */
545 *e++ = 130;
546 *e++ = 83;
547 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000548
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500549#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000550 *e++ = 53; /* DHCP Message Type */
551 *e++ = 1;
552 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000553
wdenk232c1502004-03-12 00:14:09 +0000554 *e++ = 57; /* Maximum DHCP Message Size */
555 *e++ = 2;
556 *e++ = (576 - 312 + OPT_SIZE) >> 16;
557 *e++ = (576 - 312 + OPT_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500558#endif
wdenk3861aa52002-09-27 23:19:37 +0000559
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500560#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000561 *e++ = 1; /* Subnet mask request */
562 *e++ = 4;
563 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000564#endif
565
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500566#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000567 *e++ = 3; /* Default gateway request */
568 *e++ = 4;
569 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000570#endif
571
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500572#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000573 *e++ = 6; /* Domain Name Server */
574 *e++ = 4;
575 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000576#endif
577
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500578#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000579 *e++ = 12; /* Host name request */
580 *e++ = 32;
581 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000582#endif
583
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500584#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000585 *e++ = 13; /* Boot file size */
586 *e++ = 2;
587 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000588#endif
589
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500590#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000591 *e++ = 17; /* Boot path */
592 *e++ = 32;
593 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000594#endif
595
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500596#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000597 *e++ = 40; /* NIS Domain name request */
598 *e++ = 32;
599 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000600#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000601#if defined(CONFIG_BOOTP_NTPSERVER)
602 *e++ = 42;
603 *e++ = 4;
604 e += 4;
605#endif
wdenk3861aa52002-09-27 23:19:37 +0000606
wdenk232c1502004-03-12 00:14:09 +0000607 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000608
wdenk232c1502004-03-12 00:14:09 +0000609 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000610}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500611#endif
wdenk3861aa52002-09-27 23:19:37 +0000612
613void
614BootpRequest (void)
615{
616 volatile uchar *pkt, *iphdr;
617 Bootp_t *bp;
618 int ext_len, pktlen, iplen;
619
Simon Glass573f14f2011-12-10 11:08:06 +0000620 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500621#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000622 dhcp_state = INIT;
623#endif
624
625#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
626 unsigned char bi_enetaddr[6];
627 int reg;
wdenk3861aa52002-09-27 23:19:37 +0000628 ulong tst1, tst2, sum, m_mask, m_value = 0;
629
630 if (BootpTry ==0) {
631 /* get our mac */
Mike Frysinger95823ca2009-02-11 18:23:48 -0500632 eth_getenv_enetaddr("ethaddr", bi_enetaddr);
wdenk3861aa52002-09-27 23:19:37 +0000633
Robin Getz0ebf04c2009-07-23 03:01:03 -0400634 debug("BootpRequest => Our Mac: ");
635 for (reg=0; reg<6; reg++)
636 debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':');
wdenk3861aa52002-09-27 23:19:37 +0000637
638 /* Mac-Manipulation 2 get seed1 */
639 tst1=0;
640 tst2=0;
641 for (reg=2; reg<6; reg++) {
642 tst1 = tst1 << 8;
643 tst1 = tst1 | bi_enetaddr[reg];
644 }
645 for (reg=0; reg<2; reg++) {
646 tst2 = tst2 | bi_enetaddr[reg];
647 tst2 = tst2 << 8;
648 }
649
650 seed1 = tst1^tst2;
651
652 /* Mirror seed1*/
653 m_mask=0x1;
654 for (reg=1;reg<=32;reg++) {
655 m_value |= (m_mask & seed1);
656 seed1 = seed1 >> 1;
657 m_value = m_value << 1;
658 }
659 seed1 = m_value;
660 seed2 = 0xB78D0945;
661 }
662
663 /* Random Number Generator */
664
665 for (reg=0;reg<=0;reg++) {
666 sum = seed1 + seed2;
667 if (sum < seed1 || sum < seed2)
668 sum++;
wdenk8bde7f72003-06-27 21:31:46 +0000669 seed2 = seed1;
wdenk3861aa52002-09-27 23:19:37 +0000670 seed1 = sum;
671
672 if (BootpTry<=2) { /* Start with max 1024 * 1ms */
673 sum = sum >> (22-BootpTry);
674 } else { /*After 3rd BOOTP request max 8192 * 1ms */
675 sum = sum >> 19;
676 }
677 }
678
679 printf ("Random delay: %ld ms...\n", sum);
680 for (reg=0; reg <sum; reg++) {
681 udelay(1000); /*Wait 1ms*/
682 }
683#endif /* CONFIG_BOOTP_RANDOM_DELAY */
684
685 printf("BOOTP broadcast %d\n", ++BootpTry);
686 pkt = NetTxPacket;
687 memset ((void*)pkt, 0, PKTSIZE);
688
wdenka3d991b2004-04-15 21:48:45 +0000689 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000690
691 /*
692 * Next line results in incorrect packet size being transmitted, resulting
693 * in errors in some DHCP servers, reporting missing bytes. Size must be
694 * set in packet header after extension length has been determined.
695 * C. Hallinan, DS4.COM, Inc.
696 */
697 /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
698 iphdr = pkt; /* We need this later for NetSetIP() */
699 pkt += IP_HDR_SIZE;
700
701 bp = (Bootp_t *)pkt;
702 bp->bp_op = OP_BOOTREQUEST;
703 bp->bp_htype = HWT_ETHER;
704 bp->bp_hlen = HWL_ETHER;
705 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200706 bp->bp_secs = htons(get_timer(0) / 1000);
wdenk3861aa52002-09-27 23:19:37 +0000707 NetWriteIP(&bp->bp_ciaddr, 0);
708 NetWriteIP(&bp->bp_yiaddr, 0);
709 NetWriteIP(&bp->bp_siaddr, 0);
710 NetWriteIP(&bp->bp_giaddr, 0);
711 memcpy (bp->bp_chaddr, NetOurEther, 6);
712 copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
713
714 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500715#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200716 ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
wdenk3861aa52002-09-27 23:19:37 +0000717#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200718 ext_len = BootpExtended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500719#endif
wdenk3861aa52002-09-27 23:19:37 +0000720
721 /*
722 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200723 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000724 */
725 BootpID = ((ulong)NetOurEther[2] << 24)
726 | ((ulong)NetOurEther[3] << 16)
727 | ((ulong)NetOurEther[4] << 8)
728 | (ulong)NetOurEther[5];
729 BootpID += get_timer(0);
wdenk232c1502004-03-12 00:14:09 +0000730 BootpID = htonl(BootpID);
wdenk3861aa52002-09-27 23:19:37 +0000731 NetCopyLong(&bp->bp_id, &BootpID);
732
733 /*
734 * Calculate proper packet lengths taking into account the
735 * variable size of the options field
736 */
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200737 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
wdenk3861aa52002-09-27 23:19:37 +0000738 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
739 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200740 NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000741
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500742#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000743 dhcp_state = SELECTING;
744 NetSetHandler(DhcpHandler);
745#else
746 NetSetHandler(BootpHandler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500747#endif
wdenk3861aa52002-09-27 23:19:37 +0000748 NetSendPacket(NetTxPacket, pktlen);
749}
750
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500751#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100752static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
wdenk3861aa52002-09-27 23:19:37 +0000753{
wdenk3e386912003-04-05 00:53:31 +0000754 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000755 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200756#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
757 int *to_ptr;
758#endif
wdenk3861aa52002-09-27 23:19:37 +0000759
wdenk232c1502004-03-12 00:14:09 +0000760 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000761 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000762 switch (*popt) {
763 case 1:
764 NetCopyIP (&NetOurSubnetMask, (popt + 2));
765 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500766#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000767 case 2: /* Time offset */
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200768 to_ptr = &NetTimeOffset;
769 NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000770 NetTimeOffset = ntohl (NetTimeOffset);
771 break;
772#endif
wdenk232c1502004-03-12 00:14:09 +0000773 case 3:
774 NetCopyIP (&NetOurGatewayIP, (popt + 2));
775 break;
776 case 6:
777 NetCopyIP (&NetOurDNSIP, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500778#if defined(CONFIG_BOOTP_DNS2)
wdenk232c1502004-03-12 00:14:09 +0000779 if (*(popt + 1) > 4) {
780 NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
781 }
stroesefe389a82003-08-28 14:17:32 +0000782#endif
wdenk232c1502004-03-12 00:14:09 +0000783 break;
784 case 12:
785 size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
786 memcpy (&NetOurHostName, popt + 2, size);
787 NetOurHostName[size] = 0;
788 break;
789 case 15: /* Ignore Domain Name Option */
790 break;
791 case 17:
792 size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
793 memcpy (&NetOurRootPath, popt + 2, size);
794 NetOurRootPath[size] = 0;
795 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500796#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000797 case 42: /* NTP server IP */
798 NetCopyIP (&NetNtpServerIP, (popt + 2));
799 break;
800#endif
wdenk232c1502004-03-12 00:14:09 +0000801 case 51:
802 NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
803 break;
804 case 53: /* Ignore Message Type Option */
805 break;
806 case 54:
807 NetCopyIP (&NetDHCPServerIP, (popt + 2));
808 break;
809 case 58: /* Ignore Renewal Time Option */
810 break;
811 case 59: /* Ignore Rebinding Time Option */
812 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100813 case 66: /* Ignore TFTP server name */
814 break;
815 case 67: /* vendor opt bootfile */
816 /*
817 * I can't use dhcp_vendorex_proc here because I need
818 * to write into the bootp packet - even then I had to
819 * pass the bootp packet pointer into here as the
820 * second arg
821 */
822 size = truncate_sz ("Opt Boot File",
823 sizeof(bp->bp_file),
824 oplen);
825 if (bp->bp_file[0] == '\0' && size > 0) {
826 /*
827 * only use vendor boot file if we didn't
828 * receive a boot file in the main non-vendor
829 * part of the packet - god only knows why
830 * some vendors chose not to use this perfectly
831 * good spot to store the boot file (join on
832 * Tru64 Unix) it seems mind bogglingly crazy
833 * to me
834 */
835 printf("*** WARNING: using vendor "
836 "optional boot file\n");
837 memcpy(bp->bp_file, popt + 2, size);
838 bp->bp_file[size] = '\0';
839 }
840 break;
wdenk232c1502004-03-12 00:14:09 +0000841 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500842#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000843 if (dhcp_vendorex_proc (popt))
wdenk8bde7f72003-06-27 21:31:46 +0000844 break;
wdenk3861aa52002-09-27 23:19:37 +0000845#endif
wdenk232c1502004-03-12 00:14:09 +0000846 printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
847 break;
wdenk3861aa52002-09-27 23:19:37 +0000848 }
849 popt += oplen + 2; /* Process next option */
850 }
851}
852
853static int DhcpMessageType(unsigned char *popt)
854{
855 if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
856 return -1;
857
858 popt += 4;
859 while ( *popt != 0xff ) {
860 if ( *popt == 53 ) /* DHCP Message Type */
861 return *(popt + 2);
862 popt += *(popt + 1) + 2; /* Scan through all options */
863 }
864 return -1;
865}
866
wdenk3e386912003-04-05 00:53:31 +0000867static void DhcpSendRequestPkt(Bootp_t *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000868{
869 volatile uchar *pkt, *iphdr;
870 Bootp_t *bp;
871 int pktlen, iplen, extlen;
wdenk47cd00f2003-03-06 13:39:27 +0000872 IPaddr_t OfferedIP;
wdenk3861aa52002-09-27 23:19:37 +0000873
Robin Getz0ebf04c2009-07-23 03:01:03 -0400874 debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
wdenk3861aa52002-09-27 23:19:37 +0000875 pkt = NetTxPacket;
876 memset ((void*)pkt, 0, PKTSIZE);
877
wdenka3d991b2004-04-15 21:48:45 +0000878 pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
wdenk3861aa52002-09-27 23:19:37 +0000879
880 iphdr = pkt; /* We'll need this later to set proper pkt size */
881 pkt += IP_HDR_SIZE;
882
883 bp = (Bootp_t *)pkt;
884 bp->bp_op = OP_BOOTREQUEST;
885 bp->bp_htype = HWT_ETHER;
886 bp->bp_hlen = HWL_ETHER;
887 bp->bp_hops = 0;
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200888 bp->bp_secs = htons(get_timer(0) / 1000);
Justin Flammiae5c794e2007-10-29 17:40:35 -0400889 /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
890 * the server yet */
891
Wolfgang Denkc6686702006-10-12 00:01:08 +0200892 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200893 * RFC3046 requires Relay Agents to discard packets with
894 * nonzero and offered giaddr
895 */
896 NetWriteIP(&bp->bp_giaddr, 0);
897
wdenk3861aa52002-09-27 23:19:37 +0000898 memcpy (bp->bp_chaddr, NetOurEther, 6);
899
900 /*
901 * ID is the id of the OFFER packet
902 */
903
904 NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
905
906 /*
907 * Copy options from OFFER packet if present
908 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400909
910 /* Copy offered IP into the parameters request list */
911 NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200912 extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
wdenk3861aa52002-09-27 23:19:37 +0000913
Norbert van Bolhuisc9a2aab2009-06-04 09:39:48 +0200914 pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
wdenk3861aa52002-09-27 23:19:37 +0000915 iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
916 NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
917
Robin Getz0ebf04c2009-07-23 03:01:03 -0400918 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100919#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
920 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
921#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
wdenk3861aa52002-09-27 23:19:37 +0000922 NetSendPacket(NetTxPacket, pktlen);
923}
924
925/*
926 * Handle DHCP received packets.
927 */
928static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000929DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
930 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000931{
932 Bootp_t *bp = (Bootp_t *)pkt;
933
Robin Getz0ebf04c2009-07-23 03:01:03 -0400934 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000935 src, dest, len, dhcp_state);
936
wdenk232c1502004-03-12 00:14:09 +0000937 if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
wdenk3861aa52002-09-27 23:19:37 +0000938 return;
939
Robin Getz0ebf04c2009-07-23 03:01:03 -0400940 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
wdenk3861aa52002-09-27 23:19:37 +0000941 src, dest, len, dhcp_state);
942
943 switch (dhcp_state) {
944 case SELECTING:
945 /*
946 * Wait an appropriate time for any potential DHCPOFFER packets
947 * to arrive. Then select one, and generate DHCPREQUEST response.
948 * If filename is in format we recognize, assume it is a valid
949 * OFFER from a server we want.
950 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400951 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200952#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000953 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200954 CONFIG_SYS_BOOTFILE_PREFIX,
955 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
956#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000957
Robin Getz0ebf04c2009-07-23 03:01:03 -0400958 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000959 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000960
wdenk3861aa52002-09-27 23:19:37 +0000961 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100962 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000963
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200964 NetSetTimeout(TIMEOUT, BootpTimeout);
wdenk3861aa52002-09-27 23:19:37 +0000965 DhcpSendRequestPkt(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200966#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000967 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200968#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000969
970 return;
971 break;
972 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -0400973 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +0000974
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200975 if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
wdenk3861aa52002-09-27 23:19:37 +0000976 if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100977 DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
wdenk232c1502004-03-12 00:14:09 +0000978 BootpCopyNetParams(bp); /* Store net params from reply */
wdenk3861aa52002-09-27 23:19:37 +0000979 dhcp_state = BOUND;
Mike Frysingerb6446b62009-02-17 00:00:53 -0500980 printf ("DHCP client bound to address %pI4\n", &NetOurIP);
Simon Glass573f14f2011-12-10 11:08:06 +0000981 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
982 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000983
Simon Glasse4a3d572011-10-27 06:24:32 +0000984 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000985 return;
986 }
987 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +0200988 case BOUND:
989 /* DHCP client bound to address */
990 break;
wdenk3861aa52002-09-27 23:19:37 +0000991 default:
wdenk4b9206e2004-03-23 22:14:11 +0000992 puts ("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000993 break;
994 }
995
996}
997
998void DhcpRequest(void)
999{
1000 BootpRequest();
1001}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001002#endif /* CONFIG_CMD_DHCP */