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