wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1 | /* |
| 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 |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 8 | * Copyright 2000-2004 Wolfgang Denk, wd@denx.de |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <net.h> |
| 14 | #include "bootp.h" |
| 15 | #include "tftp.h" |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 16 | #include "nfs.h" |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 17 | #ifdef CONFIG_STATUS_LED |
| 18 | #include <status_led.h> |
| 19 | #endif |
Kim Phillips | db7720b | 2012-07-05 13:19:32 +0000 | [diff] [blame] | 20 | #ifdef CONFIG_BOOTP_RANDOM_DELAY |
| 21 | #include "net_rand.h" |
| 22 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 23 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 24 | #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 25 | |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 26 | /* |
| 27 | * The timeout for the initial BOOTP/DHCP request used to be described by a |
| 28 | * counter of fixed-length timeout periods. TIMEOUT_COUNT represents |
| 29 | * that counter |
| 30 | * |
| 31 | * Now that the timeout periods are variable (exponential backoff and retry) |
| 32 | * we convert the timeout count to the absolute time it would have take to |
| 33 | * execute that many retries, and keep sending retry packets until that time |
| 34 | * is reached. |
| 35 | */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 36 | #ifndef CONFIG_NET_RETRY_COUNT |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 37 | # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 38 | #else |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 39 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 40 | #endif |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 41 | #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 42 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 43 | #define PORT_BOOTPS 67 /* BOOTP server UDP port */ |
| 44 | #define PORT_BOOTPC 68 /* BOOTP client UDP port */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 45 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 46 | #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 47 | #define CONFIG_DHCP_MIN_EXT_LEN 64 |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 50 | #ifndef CONFIG_BOOTP_ID_CACHE_SIZE |
| 51 | #define CONFIG_BOOTP_ID_CACHE_SIZE 4 |
| 52 | #endif |
| 53 | |
| 54 | ulong bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE]; |
| 55 | unsigned int bootp_num_ids; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 56 | int BootpTry; |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 57 | ulong bootp_start; |
| 58 | ulong bootp_timeout; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 59 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 60 | #if defined(CONFIG_CMD_DHCP) |
Kim Phillips | 0637059 | 2012-10-29 13:34:33 +0000 | [diff] [blame] | 61 | static dhcp_state_t dhcp_state = INIT; |
| 62 | static unsigned long dhcp_leasetime; |
| 63 | static IPaddr_t NetDHCPServerIP; |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 64 | static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 65 | unsigned len); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 66 | |
| 67 | /* For Debug */ |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 68 | #if 0 |
| 69 | static char *dhcpmsg2str(int type) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 70 | { |
| 71 | switch (type) { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 72 | case 1: return "DHCPDISCOVER"; break; |
| 73 | case 2: return "DHCPOFFER"; break; |
| 74 | case 3: return "DHCPREQUEST"; break; |
| 75 | case 4: return "DHCPDECLINE"; break; |
| 76 | case 5: return "DHCPACK"; break; |
| 77 | case 6: return "DHCPNACK"; break; |
| 78 | case 7: return "DHCPRELEASE"; break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 79 | default: return "UNKNOWN/INVALID MSG TYPE"; break; |
| 80 | } |
| 81 | } |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 82 | #endif |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 83 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 84 | |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 85 | static void bootp_add_id(ulong id) |
| 86 | { |
| 87 | if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) { |
| 88 | size_t size = sizeof(bootp_ids) - sizeof(id); |
| 89 | |
| 90 | memmove(bootp_ids, &bootp_ids[1], size); |
| 91 | bootp_ids[bootp_num_ids - 1] = id; |
| 92 | } else { |
| 93 | bootp_ids[bootp_num_ids] = id; |
| 94 | bootp_num_ids++; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static bool bootp_match_id(ulong id) |
| 99 | { |
| 100 | unsigned int i; |
| 101 | |
| 102 | for (i = 0; i < bootp_num_ids; i++) |
| 103 | if (bootp_ids[i] == id) |
| 104 | return true; |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 109 | static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) |
| 110 | { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 111 | struct Bootp_t *bp = (struct Bootp_t *) pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 112 | int retval = 0; |
| 113 | |
| 114 | if (dest != PORT_BOOTPC || src != PORT_BOOTPS) |
| 115 | retval = -1; |
Joe Hershberger | f831573 | 2012-05-23 07:58:14 +0000 | [diff] [blame] | 116 | else if (len < sizeof(struct Bootp_t) - OPT_FIELD_SIZE) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 117 | retval = -2; |
| 118 | else if (bp->bp_op != OP_BOOTREQUEST && |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 119 | bp->bp_op != OP_BOOTREPLY && |
| 120 | bp->bp_op != DHCP_OFFER && |
| 121 | bp->bp_op != DHCP_ACK && |
| 122 | bp->bp_op != DHCP_NAK) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 123 | retval = -3; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 124 | else if (bp->bp_htype != HWT_ETHER) |
| 125 | retval = -4; |
| 126 | else if (bp->bp_hlen != HWL_ETHER) |
| 127 | retval = -5; |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 128 | else if (!bootp_match_id(NetReadLong((ulong *)&bp->bp_id))) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 129 | retval = -6; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 130 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 131 | debug("Filtering pkt = %d\n", retval); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 132 | |
| 133 | return retval; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet |
| 138 | */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 139 | static void BootpCopyNetParams(struct Bootp_t *bp) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 140 | { |
Wilson Callan | 5d110f0 | 2007-07-28 10:56:13 -0400 | [diff] [blame] | 141 | #if !defined(CONFIG_BOOTP_SERVERIP) |
Joe Hershberger | 1752f0f | 2012-05-23 07:59:18 +0000 | [diff] [blame] | 142 | IPaddr_t tmp_ip; |
| 143 | |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 144 | NetCopyIP(&tmp_ip, &bp->bp_siaddr); |
| 145 | if (tmp_ip != 0) |
| 146 | NetCopyIP(&NetServerIP, &bp->bp_siaddr); |
Joe Hershberger | cb487f5 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 147 | memcpy(NetServerEther, ((struct ethernet_hdr *)NetRxPacket)->et_src, 6); |
Wilson Callan | 5d110f0 | 2007-07-28 10:56:13 -0400 | [diff] [blame] | 148 | #endif |
Joe Hershberger | 1752f0f | 2012-05-23 07:59:18 +0000 | [diff] [blame] | 149 | NetCopyIP(&NetOurIP, &bp->bp_yiaddr); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 150 | if (strlen(bp->bp_file) > 0) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 151 | copy_filename(BootFile, bp->bp_file, sizeof(BootFile)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 152 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 153 | debug("Bootfile: %s\n", BootFile); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 154 | |
| 155 | /* Propagate to environment: |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 156 | * don't delete exising entry when BOOTP / DHCP reply does |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 157 | * not contain a new value |
| 158 | */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 159 | if (*BootFile) |
| 160 | setenv("bootfile", BootFile); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 163 | static int truncate_sz(const char *name, int maxlen, int curlen) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 164 | { |
| 165 | if (curlen >= maxlen) { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 166 | printf("*** WARNING: %s is too long (%d - max: %d)" |
| 167 | " - truncated\n", name, curlen, maxlen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 168 | curlen = maxlen - 1; |
| 169 | } |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 170 | return curlen; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 173 | #if !defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 174 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 175 | static void BootpVendorFieldProcess(u8 *ext) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 176 | { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 177 | int size = *(ext + 1); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 178 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 179 | debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 180 | *(ext + 1)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 181 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 182 | NetBootFileSize = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 183 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 184 | switch (*ext) { |
| 185 | /* Fixed length fields */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 186 | case 1: /* Subnet mask */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 187 | if (NetOurSubnetMask == 0) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 188 | NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 189 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 190 | case 2: /* Time offset - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 191 | break; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 192 | /* Variable length fields */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 193 | case 3: /* Gateways list */ |
| 194 | if (NetOurGatewayIP == 0) |
| 195 | NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 196 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 197 | case 4: /* Time server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 198 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 199 | case 5: /* IEN-116 name server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 200 | break; |
| 201 | case 6: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 202 | if (NetOurDNSIP == 0) |
| 203 | NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2)); |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 204 | #if defined(CONFIG_BOOTP_DNS2) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 205 | if ((NetOurDNS2IP == 0) && (size > 4)) |
| 206 | NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 207 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 208 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 209 | case 7: /* Log server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 210 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 211 | case 8: /* Cookie/Quote server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 212 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 213 | case 9: /* LPR server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 214 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 215 | case 10: /* Impress server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 216 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 217 | case 11: /* RPL server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 218 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 219 | case 12: /* Host name */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 220 | if (NetOurHostName[0] == 0) { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 221 | size = truncate_sz("Host Name", |
| 222 | sizeof(NetOurHostName), size); |
| 223 | memcpy(&NetOurHostName, ext + 2, size); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 224 | NetOurHostName[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 225 | } |
| 226 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 227 | case 13: /* Boot file size */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 228 | if (size == 2) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 229 | NetBootFileSize = ntohs(*(ushort *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 230 | else if (size == 4) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 231 | NetBootFileSize = ntohl(*(ulong *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 232 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 233 | case 14: /* Merit dump file - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 234 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 235 | case 15: /* Domain name - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 236 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 237 | case 16: /* Swap server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 238 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 239 | case 17: /* Root path */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 240 | if (NetOurRootPath[0] == 0) { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 241 | size = truncate_sz("Root Path", |
| 242 | sizeof(NetOurRootPath), size); |
| 243 | memcpy(&NetOurRootPath, ext + 2, size); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 244 | NetOurRootPath[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 245 | } |
| 246 | break; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 247 | case 18: /* Extension path - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 248 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 249 | * This can be used to send the information of the |
| 250 | * vendor area in another file that the client can |
| 251 | * access via TFTP. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 252 | */ |
| 253 | break; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 254 | /* IP host layer fields */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 255 | case 40: /* NIS Domain name */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 256 | if (NetOurNISDomain[0] == 0) { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 257 | size = truncate_sz("NIS Domain Name", |
| 258 | sizeof(NetOurNISDomain), size); |
| 259 | memcpy(&NetOurNISDomain, ext + 2, size); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 260 | NetOurNISDomain[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 261 | } |
| 262 | break; |
Luuk Paulussen | 09e3a67 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 263 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
| 264 | case 42: /* NTP server IP */ |
| 265 | NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2)); |
| 266 | break; |
| 267 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 268 | /* Application layer fields */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 269 | case 43: /* Vendor specific info - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 270 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 271 | * Binary information to exchange specific |
| 272 | * product information. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 273 | */ |
| 274 | break; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 275 | /* Reserved (custom) fields (128..254) */ |
| 276 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 279 | static void BootpVendorProcess(u8 *ext, int size) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 280 | { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 281 | u8 *end = ext + size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 282 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 283 | debug("[BOOTP] Checking extension (%d bytes)...\n", size); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 284 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 285 | while ((ext < end) && (*ext != 0xff)) { |
| 286 | if (*ext == 0) { |
| 287 | ext++; |
| 288 | } else { |
| 289 | u8 *opt = ext; |
| 290 | |
| 291 | ext += ext[1] + 2; |
| 292 | if (ext <= end) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 293 | BootpVendorFieldProcess(opt); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 294 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 295 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 296 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 297 | debug("[BOOTP] Received fields:\n"); |
Mike Frysinger | b6446b6 | 2009-02-17 00:00:53 -0500 | [diff] [blame] | 298 | if (NetOurSubnetMask) |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 299 | debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 300 | |
Mike Frysinger | b6446b6 | 2009-02-17 00:00:53 -0500 | [diff] [blame] | 301 | if (NetOurGatewayIP) |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 302 | debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 303 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 304 | if (NetBootFileSize) |
| 305 | debug("NetBootFileSize : %d\n", NetBootFileSize); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 306 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 307 | if (NetOurHostName[0]) |
| 308 | debug("NetOurHostName : %s\n", NetOurHostName); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 309 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 310 | if (NetOurRootPath[0]) |
| 311 | debug("NetOurRootPath : %s\n", NetOurRootPath); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 312 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 313 | if (NetOurNISDomain[0]) |
| 314 | debug("NetOurNISDomain : %s\n", NetOurNISDomain); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 315 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 316 | if (NetBootFileSize) |
| 317 | debug("NetBootFileSize: %d\n", NetBootFileSize); |
Luuk Paulussen | 09e3a67 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 318 | |
| 319 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
| 320 | if (NetNtpServerIP) |
| 321 | debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP); |
| 322 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 323 | } |
Simon Glass | 0934986 | 2011-06-13 16:13:12 -0700 | [diff] [blame] | 324 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 325 | /* |
| 326 | * Handle a BOOTP received packet. |
| 327 | */ |
| 328 | static void |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 329 | BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 330 | unsigned len) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 331 | { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 332 | struct Bootp_t *bp; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 333 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 334 | debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n", |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 335 | src, dest, len, sizeof(struct Bootp_t)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 336 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 337 | bp = (struct Bootp_t *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 338 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 339 | /* Filter out pkts we don't want */ |
| 340 | if (BootpCheckPkt(pkt, dest, src, len)) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 341 | return; |
| 342 | |
| 343 | /* |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 344 | * Got a good BOOTP reply. Copy the data into our variables. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 345 | */ |
| 346 | #ifdef CONFIG_STATUS_LED |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 347 | status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 348 | #endif |
| 349 | |
| 350 | BootpCopyNetParams(bp); /* Store net parameters from reply */ |
| 351 | |
| 352 | /* Retrieve extended information (we must parse the vendor area) */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 353 | if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 354 | BootpVendorProcess((uchar *)&bp->bp_vend[4], len); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 355 | |
| 356 | NetSetTimeout(0, (thand_f *)0); |
Simon Glass | 573f14f | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 357 | bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 358 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 359 | debug("Got good BOOTP\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 360 | |
Simon Glass | e4a3d57 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 361 | net_auto_load(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 362 | } |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 363 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 364 | |
| 365 | /* |
| 366 | * Timeout on BOOTP/DHCP request. |
| 367 | */ |
| 368 | static void |
| 369 | BootpTimeout(void) |
| 370 | { |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 371 | ulong time_taken = get_timer(bootp_start); |
| 372 | |
| 373 | if (time_taken >= TIMEOUT_MS) { |
Joe Hershberger | 2c00e09 | 2012-05-23 07:59:19 +0000 | [diff] [blame] | 374 | #ifdef CONFIG_BOOTP_MAY_FAIL |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 375 | puts("\nRetry time exceeded\n"); |
benoit.thebaudeau@advans | b977aa8 | 2012-07-19 01:23:21 +0000 | [diff] [blame] | 376 | net_set_state(NETLOOP_FAIL); |
Joe Hershberger | 2c00e09 | 2012-05-23 07:59:19 +0000 | [diff] [blame] | 377 | #else |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 378 | puts("\nRetry time exceeded; starting again\n"); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 379 | NetStartAgain(); |
Joe Hershberger | 2c00e09 | 2012-05-23 07:59:19 +0000 | [diff] [blame] | 380 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 381 | } else { |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 382 | bootp_timeout *= 2; |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 383 | if (bootp_timeout > 2000) |
| 384 | bootp_timeout = 2000; |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 385 | NetSetTimeout(bootp_timeout, BootpTimeout); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 386 | BootpRequest(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Ilya Yanok | 9ace17c | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 390 | #define put_vci(e, str) \ |
| 391 | do { \ |
| 392 | size_t vci_strlen = strlen(str); \ |
| 393 | *e++ = 60; /* Vendor Class Identifier */ \ |
| 394 | *e++ = vci_strlen; \ |
| 395 | memcpy(e, str, vci_strlen); \ |
| 396 | e += vci_strlen; \ |
| 397 | } while (0) |
| 398 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 399 | /* |
| 400 | * Initialize BOOTP extension fields in the request. |
| 401 | */ |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 402 | #if defined(CONFIG_CMD_DHCP) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 403 | static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID, |
| 404 | IPaddr_t RequestedIP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 405 | { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 406 | u8 *start = e; |
| 407 | u8 *cnt; |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 408 | #if defined(CONFIG_BOOTP_PXE) |
| 409 | char *uuid; |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 410 | u16 clientarch; |
| 411 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 412 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 413 | #if defined(CONFIG_BOOTP_VENDOREX) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 414 | u8 *x; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 415 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 416 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 417 | char *hostname; |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 418 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 419 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 420 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 421 | *e++ = 130; |
| 422 | *e++ = 83; |
| 423 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 424 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 425 | *e++ = 53; /* DHCP Message Type */ |
| 426 | *e++ = 1; |
| 427 | *e++ = message_type; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 428 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 429 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 430 | *e++ = 2; |
Joe Hershberger | f831573 | 2012-05-23 07:58:14 +0000 | [diff] [blame] | 431 | *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8; |
| 432 | *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 433 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 434 | if (ServerID) { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 435 | int tmp = ntohl(ServerID); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 436 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 437 | *e++ = 54; /* ServerID */ |
| 438 | *e++ = 4; |
| 439 | *e++ = tmp >> 24; |
| 440 | *e++ = tmp >> 16; |
| 441 | *e++ = tmp >> 8; |
| 442 | *e++ = tmp & 0xff; |
| 443 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 444 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 445 | if (RequestedIP) { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 446 | int tmp = ntohl(RequestedIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 447 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 448 | *e++ = 50; /* Requested IP */ |
| 449 | *e++ = 4; |
| 450 | *e++ = tmp >> 24; |
| 451 | *e++ = tmp >> 16; |
| 452 | *e++ = tmp >> 8; |
| 453 | *e++ = tmp & 0xff; |
| 454 | } |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 455 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 456 | hostname = getenv("hostname"); |
| 457 | if (hostname) { |
| 458 | int hostnamelen = strlen(hostname); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 459 | |
| 460 | *e++ = 12; /* Hostname */ |
| 461 | *e++ = hostnamelen; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 462 | memcpy(e, hostname, hostnamelen); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 463 | e += hostnamelen; |
| 464 | } |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 465 | #endif |
| 466 | |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 467 | #if defined(CONFIG_BOOTP_PXE) |
| 468 | clientarch = CONFIG_BOOTP_PXE_CLIENTARCH; |
| 469 | *e++ = 93; /* Client System Architecture */ |
| 470 | *e++ = 2; |
| 471 | *e++ = (clientarch >> 8) & 0xff; |
| 472 | *e++ = clientarch & 0xff; |
| 473 | |
| 474 | *e++ = 94; /* Client Network Interface Identifier */ |
| 475 | *e++ = 3; |
| 476 | *e++ = 1; /* type field for UNDI */ |
| 477 | *e++ = 0; /* major revision */ |
| 478 | *e++ = 0; /* minor revision */ |
| 479 | |
| 480 | uuid = getenv("pxeuuid"); |
| 481 | |
| 482 | if (uuid) { |
| 483 | if (uuid_str_valid(uuid)) { |
| 484 | *e++ = 97; /* Client Machine Identifier */ |
| 485 | *e++ = 17; |
| 486 | *e++ = 0; /* type 0 - UUID */ |
| 487 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 488 | uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD); |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 489 | e += 16; |
| 490 | } else { |
| 491 | printf("Invalid pxeuuid: %s\n", uuid); |
| 492 | } |
| 493 | } |
Ilya Yanok | 9ace17c | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 494 | #endif |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 495 | |
Ilya Yanok | 9ace17c | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 496 | #ifdef CONFIG_BOOTP_VCI_STRING |
| 497 | put_vci(e, CONFIG_BOOTP_VCI_STRING); |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 498 | #endif |
| 499 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 500 | #if defined(CONFIG_BOOTP_VENDOREX) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 501 | x = dhcp_vendorex_prep(e); |
| 502 | if (x) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 503 | return x - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 504 | #endif |
| 505 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 506 | *e++ = 55; /* Parameter Request List */ |
| 507 | cnt = e++; /* Pointer to count of requested items */ |
| 508 | *cnt = 0; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 509 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 510 | *e++ = 1; /* Subnet Mask */ |
| 511 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 512 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 513 | #if defined(CONFIG_BOOTP_TIMEOFFSET) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 514 | *e++ = 2; |
| 515 | *cnt += 1; |
| 516 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 517 | #if defined(CONFIG_BOOTP_GATEWAY) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 518 | *e++ = 3; /* Router Option */ |
| 519 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 520 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 521 | #if defined(CONFIG_BOOTP_DNS) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 522 | *e++ = 6; /* DNS Server(s) */ |
| 523 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 524 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 525 | #if defined(CONFIG_BOOTP_HOSTNAME) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 526 | *e++ = 12; /* Hostname */ |
| 527 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 528 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 529 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 530 | *e++ = 13; /* Boot File Size */ |
| 531 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 532 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 533 | #if defined(CONFIG_BOOTP_BOOTPATH) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 534 | *e++ = 17; /* Boot path */ |
| 535 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 536 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 537 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 538 | *e++ = 40; /* NIS Domain name request */ |
| 539 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 540 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 541 | #if defined(CONFIG_BOOTP_NTPSERVER) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 542 | *e++ = 42; |
| 543 | *cnt += 1; |
| 544 | #endif |
Jason Liu | 258ccd6 | 2010-11-14 12:23:09 +0800 | [diff] [blame] | 545 | /* no options, so back up to avoid sending an empty request list */ |
| 546 | if (*cnt == 0) |
| 547 | e -= 2; |
| 548 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 549 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 550 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 551 | /* Pad to minimal length */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 552 | #ifdef CONFIG_DHCP_MIN_EXT_LEN |
Simon Glass | 21076f6 | 2011-02-02 15:03:28 -0800 | [diff] [blame] | 553 | while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 554 | *e++ = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 555 | #endif |
| 556 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 557 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 560 | #else |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 561 | /* |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 562 | * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 563 | */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 564 | static int BootpExtended(u8 *e) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 565 | { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 566 | u8 *start = e; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 567 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 568 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 569 | *e++ = 130; |
| 570 | *e++ = 83; |
| 571 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 572 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 573 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 574 | *e++ = 53; /* DHCP Message Type */ |
| 575 | *e++ = 1; |
| 576 | *e++ = DHCP_DISCOVER; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 577 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 578 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 579 | *e++ = 2; |
Joe Hershberger | f831573 | 2012-05-23 07:58:14 +0000 | [diff] [blame] | 580 | *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16; |
| 581 | *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 582 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 583 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 584 | #if defined(CONFIG_BOOTP_VCI_STRING) || \ |
| 585 | (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)) |
| 586 | #ifdef CONFIG_SPL_BUILD |
| 587 | put_vci(e, CONFIG_SPL_NET_VCI_STRING); |
| 588 | #else |
Ilya Yanok | 9ace17c | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 589 | put_vci(e, CONFIG_BOOTP_VCI_STRING); |
| 590 | #endif |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 591 | #endif |
Ilya Yanok | 9ace17c | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 592 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 593 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 594 | *e++ = 1; /* Subnet mask request */ |
| 595 | *e++ = 4; |
| 596 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 597 | #endif |
| 598 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 599 | #if defined(CONFIG_BOOTP_GATEWAY) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 600 | *e++ = 3; /* Default gateway request */ |
| 601 | *e++ = 4; |
| 602 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 603 | #endif |
| 604 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 605 | #if defined(CONFIG_BOOTP_DNS) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 606 | *e++ = 6; /* Domain Name Server */ |
| 607 | *e++ = 4; |
| 608 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 609 | #endif |
| 610 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 611 | #if defined(CONFIG_BOOTP_HOSTNAME) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 612 | *e++ = 12; /* Host name request */ |
| 613 | *e++ = 32; |
| 614 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 615 | #endif |
| 616 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 617 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 618 | *e++ = 13; /* Boot file size */ |
| 619 | *e++ = 2; |
| 620 | e += 2; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 621 | #endif |
| 622 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 623 | #if defined(CONFIG_BOOTP_BOOTPATH) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 624 | *e++ = 17; /* Boot path */ |
| 625 | *e++ = 32; |
| 626 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 627 | #endif |
| 628 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 629 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 630 | *e++ = 40; /* NIS Domain name request */ |
| 631 | *e++ = 32; |
| 632 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 633 | #endif |
Luuk Paulussen | 09e3a67 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 634 | #if defined(CONFIG_BOOTP_NTPSERVER) |
| 635 | *e++ = 42; |
| 636 | *e++ = 4; |
| 637 | e += 4; |
| 638 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 639 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 640 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 641 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 642 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 643 | } |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 644 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 645 | |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 646 | void BootpReset(void) |
| 647 | { |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 648 | bootp_num_ids = 0; |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 649 | BootpTry = 0; |
| 650 | bootp_start = get_timer(0); |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 651 | bootp_timeout = 250; |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 652 | } |
| 653 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 654 | void |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 655 | BootpRequest(void) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 656 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 657 | uchar *pkt, *iphdr; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 658 | struct Bootp_t *bp; |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 659 | int extlen, pktlen, iplen; |
| 660 | int eth_hdr_size; |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 661 | #ifdef CONFIG_BOOTP_RANDOM_DELAY |
Pavel Machek | 8e8d73b | 2014-07-11 11:39:37 +0200 | [diff] [blame] | 662 | ulong rand_ms; |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 663 | #endif |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 664 | ulong BootpID; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 665 | |
Simon Glass | 573f14f | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 666 | bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 667 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 668 | dhcp_state = INIT; |
| 669 | #endif |
| 670 | |
| 671 | #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 672 | if (BootpTry == 0) |
| 673 | srand_mac(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 674 | |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 675 | if (BootpTry <= 2) /* Start with max 1024 * 1ms */ |
| 676 | rand_ms = rand() >> (22 - BootpTry); |
| 677 | else /* After 3rd BOOTP request max 8192 * 1ms */ |
| 678 | rand_ms = rand() >> 19; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 679 | |
Joe Hershberger | eafc8db | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 680 | printf("Random delay: %ld ms...\n", rand_ms); |
Pavel Machek | 8e8d73b | 2014-07-11 11:39:37 +0200 | [diff] [blame] | 681 | mdelay(rand_ms); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 682 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 683 | #endif /* CONFIG_BOOTP_RANDOM_DELAY */ |
| 684 | |
| 685 | printf("BOOTP broadcast %d\n", ++BootpTry); |
| 686 | pkt = NetTxPacket; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 687 | memset((void *)pkt, 0, PKTSIZE); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 688 | |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 689 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); |
| 690 | pkt += eth_hdr_size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 691 | |
| 692 | /* |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 693 | * Next line results in incorrect packet size being transmitted, |
| 694 | * resulting in errors in some DHCP servers, reporting missing bytes. |
| 695 | * Size must be set in packet header after extension length has been |
| 696 | * determined. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 697 | * C. Hallinan, DS4.COM, Inc. |
| 698 | */ |
Joe Hershberger | 4b11c91 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 699 | /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 700 | sizeof (struct Bootp_t)); */ |
Joe Hershberger | 4b11c91 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 701 | iphdr = pkt; /* We need this later for net_set_udp_header() */ |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 702 | pkt += IP_UDP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 703 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 704 | bp = (struct Bootp_t *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 705 | bp->bp_op = OP_BOOTREQUEST; |
| 706 | bp->bp_htype = HWT_ETHER; |
| 707 | bp->bp_hlen = HWL_ETHER; |
| 708 | bp->bp_hops = 0; |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 709 | bp->bp_secs = htons(get_timer(0) / 1000); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 710 | NetWriteIP(&bp->bp_ciaddr, 0); |
| 711 | NetWriteIP(&bp->bp_yiaddr, 0); |
| 712 | NetWriteIP(&bp->bp_siaddr, 0); |
| 713 | NetWriteIP(&bp->bp_giaddr, 0); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 714 | memcpy(bp->bp_chaddr, NetOurEther, 6); |
| 715 | copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 716 | |
| 717 | /* Request additional information from the BOOTP/DHCP server */ |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 718 | #if defined(CONFIG_CMD_DHCP) |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 719 | extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 720 | #else |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 721 | extlen = BootpExtended((u8 *)bp->bp_vend); |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 722 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 723 | |
| 724 | /* |
| 725 | * Bootp ID is the lower 4 bytes of our ethernet address |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 726 | * plus the current time in ms. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 727 | */ |
| 728 | BootpID = ((ulong)NetOurEther[2] << 24) |
| 729 | | ((ulong)NetOurEther[3] << 16) |
| 730 | | ((ulong)NetOurEther[4] << 8) |
| 731 | | (ulong)NetOurEther[5]; |
| 732 | BootpID += get_timer(0); |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 733 | BootpID = htonl(BootpID); |
| 734 | bootp_add_id(BootpID); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 735 | NetCopyLong(&bp->bp_id, &BootpID); |
| 736 | |
| 737 | /* |
| 738 | * Calculate proper packet lengths taking into account the |
| 739 | * variable size of the options field |
| 740 | */ |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 741 | iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; |
| 742 | pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; |
Joe Hershberger | 4b11c91 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 743 | net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 744 | NetSetTimeout(bootp_timeout, BootpTimeout); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 745 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 746 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 747 | dhcp_state = SELECTING; |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 748 | net_set_udp_handler(DhcpHandler); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 749 | #else |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 750 | net_set_udp_handler(BootpHandler); |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 751 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 752 | NetSendPacket(NetTxPacket, pktlen); |
| 753 | } |
| 754 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 755 | #if defined(CONFIG_CMD_DHCP) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 756 | static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 757 | { |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 758 | uchar *end = popt + BOOTP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 759 | int oplen, size; |
Wolfgang Denk | d8d8724 | 2009-09-11 09:05:32 +0200 | [diff] [blame] | 760 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
| 761 | int *to_ptr; |
| 762 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 763 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 764 | while (popt < end && *popt != 0xff) { |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 765 | oplen = *(popt + 1); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 766 | switch (*popt) { |
| 767 | case 1: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 768 | NetCopyIP(&NetOurSubnetMask, (popt + 2)); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 769 | break; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 770 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 771 | case 2: /* Time offset */ |
Wolfgang Denk | d8d8724 | 2009-09-11 09:05:32 +0200 | [diff] [blame] | 772 | to_ptr = &NetTimeOffset; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 773 | NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2)); |
| 774 | NetTimeOffset = ntohl(NetTimeOffset); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 775 | break; |
| 776 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 777 | case 3: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 778 | NetCopyIP(&NetOurGatewayIP, (popt + 2)); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 779 | break; |
| 780 | case 6: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 781 | NetCopyIP(&NetOurDNSIP, (popt + 2)); |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 782 | #if defined(CONFIG_BOOTP_DNS2) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 783 | if (*(popt + 1) > 4) |
| 784 | NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4)); |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 785 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 786 | break; |
| 787 | case 12: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 788 | size = truncate_sz("Host Name", |
| 789 | sizeof(NetOurHostName), oplen); |
| 790 | memcpy(&NetOurHostName, popt + 2, size); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 791 | NetOurHostName[size] = 0; |
| 792 | break; |
| 793 | case 15: /* Ignore Domain Name Option */ |
| 794 | break; |
| 795 | case 17: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 796 | size = truncate_sz("Root Path", |
| 797 | sizeof(NetOurRootPath), oplen); |
| 798 | memcpy(&NetOurRootPath, popt + 2, size); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 799 | NetOurRootPath[size] = 0; |
| 800 | break; |
Brian Rzycki | ee0f60d | 2012-09-11 09:22:53 +0000 | [diff] [blame] | 801 | case 28: /* Ignore Broadcast Address Option */ |
| 802 | break; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 803 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 804 | case 42: /* NTP server IP */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 805 | NetCopyIP(&NetNtpServerIP, (popt + 2)); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 806 | break; |
| 807 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 808 | case 51: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 809 | NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2)); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 810 | break; |
| 811 | case 53: /* Ignore Message Type Option */ |
| 812 | break; |
| 813 | case 54: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 814 | NetCopyIP(&NetDHCPServerIP, (popt + 2)); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 815 | break; |
| 816 | case 58: /* Ignore Renewal Time Option */ |
| 817 | break; |
| 818 | case 59: /* Ignore Rebinding Time Option */ |
| 819 | break; |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 820 | case 66: /* Ignore TFTP server name */ |
| 821 | break; |
| 822 | case 67: /* vendor opt bootfile */ |
| 823 | /* |
| 824 | * I can't use dhcp_vendorex_proc here because I need |
| 825 | * to write into the bootp packet - even then I had to |
| 826 | * pass the bootp packet pointer into here as the |
| 827 | * second arg |
| 828 | */ |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 829 | size = truncate_sz("Opt Boot File", |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 830 | sizeof(bp->bp_file), |
| 831 | oplen); |
| 832 | if (bp->bp_file[0] == '\0' && size > 0) { |
| 833 | /* |
| 834 | * only use vendor boot file if we didn't |
| 835 | * receive a boot file in the main non-vendor |
| 836 | * part of the packet - god only knows why |
| 837 | * some vendors chose not to use this perfectly |
| 838 | * good spot to store the boot file (join on |
| 839 | * Tru64 Unix) it seems mind bogglingly crazy |
| 840 | * to me |
| 841 | */ |
| 842 | printf("*** WARNING: using vendor " |
| 843 | "optional boot file\n"); |
| 844 | memcpy(bp->bp_file, popt + 2, size); |
| 845 | bp->bp_file[size] = '\0'; |
| 846 | } |
| 847 | break; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 848 | default: |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 849 | #if defined(CONFIG_BOOTP_VENDOREX) |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 850 | if (dhcp_vendorex_proc(popt)) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 851 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 852 | #endif |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 853 | printf("*** Unhandled DHCP Option in OFFER/ACK:" |
| 854 | " %d\n", *popt); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 855 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 856 | } |
| 857 | popt += oplen + 2; /* Process next option */ |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static int DhcpMessageType(unsigned char *popt) |
| 862 | { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 863 | if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC)) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 864 | return -1; |
| 865 | |
| 866 | popt += 4; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 867 | while (*popt != 0xff) { |
| 868 | if (*popt == 53) /* DHCP Message Type */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 869 | return *(popt + 2); |
| 870 | popt += *(popt + 1) + 2; /* Scan through all options */ |
| 871 | } |
| 872 | return -1; |
| 873 | } |
| 874 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 875 | static void DhcpSendRequestPkt(struct Bootp_t *bp_offer) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 876 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 877 | uchar *pkt, *iphdr; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 878 | struct Bootp_t *bp; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 879 | int pktlen, iplen, extlen; |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 880 | int eth_hdr_size; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 881 | IPaddr_t OfferedIP; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 882 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 883 | debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 884 | pkt = NetTxPacket; |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 885 | memset((void *)pkt, 0, PKTSIZE); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 886 | |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 887 | eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_IP); |
| 888 | pkt += eth_hdr_size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 889 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 890 | iphdr = pkt; /* We'll need this later to set proper pkt size */ |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 891 | pkt += IP_UDP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 892 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 893 | bp = (struct Bootp_t *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 894 | bp->bp_op = OP_BOOTREQUEST; |
| 895 | bp->bp_htype = HWT_ETHER; |
| 896 | bp->bp_hlen = HWL_ETHER; |
| 897 | bp->bp_hops = 0; |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 898 | bp->bp_secs = htons(get_timer(0) / 1000); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 899 | /* Do not set the client IP, your IP, or server IP yet, since it |
| 900 | * hasn't been ACK'ed by the server yet */ |
Justin Flammia | e5c794e | 2007-10-29 17:40:35 -0400 | [diff] [blame] | 901 | |
Wolfgang Denk | c668670 | 2006-10-12 00:01:08 +0200 | [diff] [blame] | 902 | /* |
Wolfgang Denk | d82718f | 2006-10-09 01:26:14 +0200 | [diff] [blame] | 903 | * RFC3046 requires Relay Agents to discard packets with |
| 904 | * nonzero and offered giaddr |
| 905 | */ |
| 906 | NetWriteIP(&bp->bp_giaddr, 0); |
| 907 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 908 | memcpy(bp->bp_chaddr, NetOurEther, 6); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 909 | |
| 910 | /* |
| 911 | * ID is the id of the OFFER packet |
| 912 | */ |
| 913 | |
| 914 | NetCopyLong(&bp->bp_id, &bp_offer->bp_id); |
| 915 | |
| 916 | /* |
| 917 | * Copy options from OFFER packet if present |
| 918 | */ |
Justin Flammia | e5c794e | 2007-10-29 17:40:35 -0400 | [diff] [blame] | 919 | |
| 920 | /* Copy offered IP into the parameters request list */ |
| 921 | NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 922 | extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, |
| 923 | NetDHCPServerIP, OfferedIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 924 | |
Joe Hershberger | ae446f5 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 925 | iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; |
| 926 | pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; |
Joe Hershberger | 4b11c91 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 927 | net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 928 | |
Aras Vaichas | d9a2f41 | 2008-03-26 09:43:57 +1100 | [diff] [blame] | 929 | #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY |
| 930 | udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); |
| 931 | #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */ |
Joe Hershberger | f962322 | 2012-05-23 07:59:11 +0000 | [diff] [blame] | 932 | debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 933 | NetSendPacket(NetTxPacket, pktlen); |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Handle DHCP received packets. |
| 938 | */ |
| 939 | static void |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 940 | DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 941 | unsigned len) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 942 | { |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 943 | struct Bootp_t *bp = (struct Bootp_t *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 944 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 945 | debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 946 | src, dest, len, dhcp_state); |
| 947 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 948 | /* Filter out pkts we don't want */ |
| 949 | if (BootpCheckPkt(pkt, dest, src, len)) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 950 | return; |
| 951 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 952 | debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:" |
| 953 | " %d\n", src, dest, len, dhcp_state); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 954 | |
| 955 | switch (dhcp_state) { |
| 956 | case SELECTING: |
| 957 | /* |
| 958 | * Wait an appropriate time for any potential DHCPOFFER packets |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 959 | * to arrive. Then select one, and generate DHCPREQUEST |
| 960 | * response. If filename is in format we recognize, assume it |
| 961 | * is a valid OFFER from a server we want. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 962 | */ |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 963 | debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 964 | #ifdef CONFIG_SYS_BOOTFILE_PREFIX |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 965 | if (strncmp(bp->bp_file, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 966 | CONFIG_SYS_BOOTFILE_PREFIX, |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 967 | strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 968 | #endif /* CONFIG_SYS_BOOTFILE_PREFIX */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 969 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 970 | debug("TRANSITIONING TO REQUESTING STATE\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 971 | dhcp_state = REQUESTING; |
stroese | 759a51b | 2003-04-10 13:26:44 +0000 | [diff] [blame] | 972 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 973 | if (NetReadLong((ulong *)&bp->bp_vend[0]) == |
| 974 | htonl(BOOTP_VENDOR_MAGIC)) |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 975 | DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 976 | |
Stephen Warren | f59be6e | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 977 | NetSetTimeout(5000, BootpTimeout); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 978 | DhcpSendRequestPkt(bp); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 979 | #ifdef CONFIG_SYS_BOOTFILE_PREFIX |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 980 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 981 | #endif /* CONFIG_SYS_BOOTFILE_PREFIX */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 982 | |
| 983 | return; |
| 984 | break; |
| 985 | case REQUESTING: |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 986 | debug("DHCP State: REQUESTING\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 987 | |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 988 | if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) { |
| 989 | if (NetReadLong((ulong *)&bp->bp_vend[0]) == |
| 990 | htonl(BOOTP_VENDOR_MAGIC)) |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 991 | DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 992 | /* Store net params from reply */ |
| 993 | BootpCopyNetParams(bp); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 994 | dhcp_state = BOUND; |
Thierry Reding | 92ac8ac | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 995 | printf("DHCP client bound to address %pI4 (%lu ms)\n", |
| 996 | &NetOurIP, get_timer(bootp_start)); |
Simon Glass | 573f14f | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 997 | bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 998 | "bootp_stop"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 999 | |
Simon Glass | e4a3d57 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 1000 | net_auto_load(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1001 | return; |
| 1002 | } |
| 1003 | break; |
Remy Bohmer | 51dfe13 | 2008-08-20 11:30:28 +0200 | [diff] [blame] | 1004 | case BOUND: |
| 1005 | /* DHCP client bound to address */ |
| 1006 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1007 | default: |
Joe Hershberger | 3090b7e3 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1008 | puts("DHCP: INVALID STATE\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | } |
| 1013 | |
| 1014 | void DhcpRequest(void) |
| 1015 | { |
| 1016 | BootpRequest(); |
| 1017 | } |
Wolfgang Denk | 992742a | 2007-11-03 23:09:27 +0100 | [diff] [blame] | 1018 | #endif /* CONFIG_CMD_DHCP */ |