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 | 29e7f5a | 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> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 12 | #include <bootstage.h> |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 13 | #include <command.h> |
Simon Glass | 6eaea25 | 2019-08-01 09:46:48 -0600 | [diff] [blame] | 14 | #include <env.h> |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 15 | #include <efi_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 17 | #include <net.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 18 | #include <rand.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 19 | #include <uuid.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 20 | #include <linux/delay.h> |
Lukasz Majewski | 2118592 | 2015-08-24 00:21:43 +0200 | [diff] [blame] | 21 | #include <net/tftp.h> |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 22 | #include "bootp.h" |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_LED_STATUS |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 24 | #include <status_led.h> |
| 25 | #endif |
Kim Phillips | 576175b | 2012-07-05 13:19:32 +0000 | [diff] [blame] | 26 | #ifdef CONFIG_BOOTP_RANDOM_DELAY |
| 27 | #include "net_rand.h" |
| 28 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 29 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 30 | #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 31 | |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 32 | /* |
| 33 | * The timeout for the initial BOOTP/DHCP request used to be described by a |
| 34 | * counter of fixed-length timeout periods. TIMEOUT_COUNT represents |
| 35 | * that counter |
| 36 | * |
| 37 | * Now that the timeout periods are variable (exponential backoff and retry) |
| 38 | * we convert the timeout count to the absolute time it would have take to |
| 39 | * execute that many retries, and keep sending retry packets until that time |
| 40 | * is reached. |
| 41 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 42 | #ifndef CONFIG_NET_RETRY_COUNT |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 43 | # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 44 | #else |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 45 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 46 | #endif |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 47 | #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 48 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 49 | #define PORT_BOOTPS 67 /* BOOTP server UDP port */ |
| 50 | #define PORT_BOOTPC 68 /* BOOTP client UDP port */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 51 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 52 | #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 53 | #define CONFIG_DHCP_MIN_EXT_LEN 64 |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 56 | #ifndef CONFIG_BOOTP_ID_CACHE_SIZE |
| 57 | #define CONFIG_BOOTP_ID_CACHE_SIZE 4 |
| 58 | #endif |
| 59 | |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 60 | u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE]; |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 61 | unsigned int bootp_num_ids; |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 62 | int bootp_try; |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 63 | ulong bootp_start; |
| 64 | ulong bootp_timeout; |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 65 | char net_nis_domain[32] = {0,}; /* Our NIS domain */ |
| 66 | char net_hostname[32] = {0,}; /* Our hostname */ |
| 67 | char net_root_path[64] = {0,}; /* Our bootpath */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 68 | |
Alexandre Messier | 1597132 | 2016-02-01 17:08:57 -0500 | [diff] [blame] | 69 | static ulong time_taken_max; |
| 70 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 71 | #if defined(CONFIG_CMD_DHCP) |
Kim Phillips | 40c2c03 | 2012-10-29 13:34:33 +0000 | [diff] [blame] | 72 | static dhcp_state_t dhcp_state = INIT; |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 73 | static u32 dhcp_leasetime; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 74 | static struct in_addr dhcp_server_ip; |
Stefan Brüns | 345cb83 | 2015-09-04 00:31:48 +0200 | [diff] [blame] | 75 | static u8 dhcp_option_overload; |
| 76 | #define OVERLOAD_FILE 1 |
| 77 | #define OVERLOAD_SNAME 2 |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 78 | static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 79 | unsigned src, unsigned len); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 80 | |
| 81 | /* For Debug */ |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 82 | #if 0 |
| 83 | static char *dhcpmsg2str(int type) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 84 | { |
| 85 | switch (type) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 86 | case 1: return "DHCPDISCOVER"; break; |
| 87 | case 2: return "DHCPOFFER"; break; |
| 88 | case 3: return "DHCPREQUEST"; break; |
| 89 | case 4: return "DHCPDECLINE"; break; |
| 90 | case 5: return "DHCPACK"; break; |
| 91 | case 6: return "DHCPNACK"; break; |
| 92 | case 7: return "DHCPRELEASE"; break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 93 | default: return "UNKNOWN/INVALID MSG TYPE"; break; |
| 94 | } |
| 95 | } |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 96 | #endif |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 97 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 98 | |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 99 | static void bootp_add_id(ulong id) |
| 100 | { |
| 101 | if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) { |
| 102 | size_t size = sizeof(bootp_ids) - sizeof(id); |
| 103 | |
| 104 | memmove(bootp_ids, &bootp_ids[1], size); |
| 105 | bootp_ids[bootp_num_ids - 1] = id; |
| 106 | } else { |
| 107 | bootp_ids[bootp_num_ids] = id; |
| 108 | bootp_num_ids++; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static bool bootp_match_id(ulong id) |
| 113 | { |
| 114 | unsigned int i; |
| 115 | |
| 116 | for (i = 0; i < bootp_num_ids; i++) |
| 117 | if (bootp_ids[i] == id) |
| 118 | return true; |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
Stefan Brüns | a48cd93 | 2015-08-27 23:53:26 +0200 | [diff] [blame] | 123 | static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src, |
| 124 | unsigned len) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 125 | { |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 126 | struct bootp_hdr *bp = (struct bootp_hdr *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 127 | int retval = 0; |
| 128 | |
| 129 | if (dest != PORT_BOOTPC || src != PORT_BOOTPS) |
| 130 | retval = -1; |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 131 | else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 132 | retval = -2; |
Stefan Brüns | a48cd93 | 2015-08-27 23:53:26 +0200 | [diff] [blame] | 133 | else if (bp->bp_op != OP_BOOTREPLY) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 134 | retval = -3; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 135 | else if (bp->bp_htype != HWT_ETHER) |
| 136 | retval = -4; |
| 137 | else if (bp->bp_hlen != HWL_ETHER) |
| 138 | retval = -5; |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 139 | else if (!bootp_match_id(net_read_u32(&bp->bp_id))) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 140 | retval = -6; |
Anton Persson | bf27a4c | 2016-03-17 09:38:21 +0100 | [diff] [blame] | 141 | else if (memcmp(bp->bp_chaddr, net_ethaddr, HWL_ETHER) != 0) |
| 142 | retval = -7; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 143 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 144 | debug("Filtering pkt = %d\n", retval); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 145 | |
| 146 | return retval; |
| 147 | } |
| 148 | |
Lyle Franklin | 73fcbc7 | 2019-08-05 06:23:42 -0400 | [diff] [blame] | 149 | static void store_bootp_params(struct bootp_hdr *bp) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 150 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 151 | struct in_addr tmp_ip; |
Alexander Graf | 427e695 | 2018-06-15 10:29:28 +0200 | [diff] [blame] | 152 | bool overwrite_serverip = true; |
| 153 | |
Simon Glass | 297fc20 | 2021-12-18 11:27:52 -0700 | [diff] [blame] | 154 | if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) |
| 155 | return; |
| 156 | |
Alexander Graf | 427e695 | 2018-06-15 10:29:28 +0200 | [diff] [blame] | 157 | #if defined(CONFIG_BOOTP_PREFER_SERVERIP) |
| 158 | overwrite_serverip = false; |
| 159 | #endif |
Joe Hershberger | 9d37a58 | 2012-05-23 07:59:18 +0000 | [diff] [blame] | 160 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 161 | net_copy_ip(&tmp_ip, &bp->bp_siaddr); |
Alexander Graf | 427e695 | 2018-06-15 10:29:28 +0200 | [diff] [blame] | 162 | if (tmp_ip.s_addr != 0 && (overwrite_serverip || !net_server_ip.s_addr)) |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 163 | net_copy_ip(&net_server_ip, &bp->bp_siaddr); |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 164 | memcpy(net_server_ethaddr, |
| 165 | ((struct ethernet_hdr *)net_rx_packet)->et_src, 6); |
Stefan Brüns | 345cb83 | 2015-09-04 00:31:48 +0200 | [diff] [blame] | 166 | if ( |
| 167 | #if defined(CONFIG_CMD_DHCP) |
| 168 | !(dhcp_option_overload & OVERLOAD_FILE) && |
| 169 | #endif |
Alexander Graf | f43bf5d | 2018-06-15 10:29:27 +0200 | [diff] [blame] | 170 | (strlen(bp->bp_file) > 0) && |
| 171 | !net_boot_file_name_explicit) { |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 172 | copy_filename(net_boot_file_name, bp->bp_file, |
| 173 | sizeof(net_boot_file_name)); |
Stefan Brüns | 345cb83 | 2015-09-04 00:31:48 +0200 | [diff] [blame] | 174 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 175 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 176 | debug("net_boot_file_name: %s\n", net_boot_file_name); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 177 | |
| 178 | /* Propagate to environment: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 179 | * don't delete exising entry when BOOTP / DHCP reply does |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 180 | * not contain a new value |
| 181 | */ |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 182 | if (*net_boot_file_name) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 183 | env_set("bootfile", net_boot_file_name); |
Lyle Franklin | 73fcbc7 | 2019-08-05 06:23:42 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet |
| 188 | */ |
| 189 | static void store_net_params(struct bootp_hdr *bp) |
| 190 | { |
| 191 | #if !defined(CONFIG_SERVERIP_FROM_PROXYDHCP) |
| 192 | store_bootp_params(bp); |
| 193 | #endif |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 194 | net_copy_ip(&net_ip, &bp->bp_yiaddr); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 197 | static int truncate_sz(const char *name, int maxlen, int curlen) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 198 | { |
| 199 | if (curlen >= maxlen) { |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 200 | printf("*** WARNING: %s is too long (%d - max: %d)" |
| 201 | " - truncated\n", name, curlen, maxlen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 202 | curlen = maxlen - 1; |
| 203 | } |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 204 | return curlen; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 207 | #if !defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 208 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 209 | static void bootp_process_vendor_field(u8 *ext) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 210 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 211 | int size = *(ext + 1); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 212 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 213 | debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 214 | *(ext + 1)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 215 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 216 | net_boot_file_expected_size_in_blocks = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 217 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 218 | switch (*ext) { |
| 219 | /* Fixed length fields */ |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 220 | case 1: /* Subnet mask */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 221 | if (net_netmask.s_addr == 0) |
| 222 | net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 223 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 224 | case 2: /* Time offset - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 225 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 226 | /* Variable length fields */ |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 227 | case 3: /* Gateways list */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 228 | if (net_gateway.s_addr == 0) |
| 229 | net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 230 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 231 | case 4: /* Time server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 232 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 233 | case 5: /* IEN-116 name server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 234 | break; |
| 235 | case 6: |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 236 | if (net_dns_server.s_addr == 0) |
| 237 | net_copy_ip(&net_dns_server, |
| 238 | (struct in_addr *)(ext + 2)); |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 239 | #if defined(CONFIG_BOOTP_DNS2) |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 240 | if ((net_dns_server2.s_addr == 0) && (size > 4)) |
| 241 | net_copy_ip(&net_dns_server2, |
| 242 | (struct in_addr *)(ext + 2 + 4)); |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 243 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 244 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 245 | case 7: /* Log server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 246 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 247 | case 8: /* Cookie/Quote server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 248 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 249 | case 9: /* LPR server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 250 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 251 | case 10: /* Impress server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 252 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 253 | case 11: /* RPL server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 254 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 255 | case 12: /* Host name */ |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 256 | if (net_hostname[0] == 0) { |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 257 | size = truncate_sz("Host Name", |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 258 | sizeof(net_hostname), size); |
| 259 | memcpy(&net_hostname, ext + 2, size); |
| 260 | net_hostname[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 261 | } |
| 262 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 263 | case 13: /* Boot file size */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 264 | if (size == 2) |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 265 | net_boot_file_expected_size_in_blocks = |
| 266 | ntohs(*(ushort *)(ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 267 | else if (size == 4) |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 268 | net_boot_file_expected_size_in_blocks = |
| 269 | ntohl(*(ulong *)(ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 270 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 271 | case 14: /* Merit dump file - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 272 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 273 | case 15: /* Domain name - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 274 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 275 | case 16: /* Swap server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 276 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 277 | case 17: /* Root path */ |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 278 | if (net_root_path[0] == 0) { |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 279 | size = truncate_sz("Root Path", |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 280 | sizeof(net_root_path), size); |
| 281 | memcpy(&net_root_path, ext + 2, size); |
| 282 | net_root_path[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 283 | } |
| 284 | break; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 285 | case 18: /* Extension path - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 286 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 287 | * This can be used to send the information of the |
| 288 | * vendor area in another file that the client can |
| 289 | * access via TFTP. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 290 | */ |
| 291 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 292 | /* IP host layer fields */ |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 293 | case 40: /* NIS Domain name */ |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 294 | if (net_nis_domain[0] == 0) { |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 295 | size = truncate_sz("NIS Domain Name", |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 296 | sizeof(net_nis_domain), size); |
| 297 | memcpy(&net_nis_domain, ext + 2, size); |
| 298 | net_nis_domain[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 299 | } |
| 300 | break; |
Luuk Paulussen | 6380e01 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 301 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
| 302 | case 42: /* NTP server IP */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 303 | net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2)); |
Luuk Paulussen | 6380e01 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 304 | break; |
| 305 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 306 | /* Application layer fields */ |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 307 | case 43: /* Vendor specific info - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 308 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 309 | * Binary information to exchange specific |
| 310 | * product information. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 311 | */ |
| 312 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 313 | /* Reserved (custom) fields (128..254) */ |
| 314 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 317 | static void bootp_process_vendor(u8 *ext, int size) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 318 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 319 | u8 *end = ext + size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 320 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 321 | debug("[BOOTP] Checking extension (%d bytes)...\n", size); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 322 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 323 | while ((ext < end) && (*ext != 0xff)) { |
| 324 | if (*ext == 0) { |
| 325 | ext++; |
| 326 | } else { |
| 327 | u8 *opt = ext; |
| 328 | |
| 329 | ext += ext[1] + 2; |
| 330 | if (ext <= end) |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 331 | bootp_process_vendor_field(opt); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 332 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 333 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 334 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 335 | debug("[BOOTP] Received fields:\n"); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 336 | if (net_netmask.s_addr) |
| 337 | debug("net_netmask : %pI4\n", &net_netmask); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 338 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 339 | if (net_gateway.s_addr) |
| 340 | debug("net_gateway : %pI4", &net_gateway); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 341 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 342 | if (net_boot_file_expected_size_in_blocks) |
| 343 | debug("net_boot_file_expected_size_in_blocks : %d\n", |
| 344 | net_boot_file_expected_size_in_blocks); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 345 | |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 346 | if (net_hostname[0]) |
| 347 | debug("net_hostname : %s\n", net_hostname); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 348 | |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 349 | if (net_root_path[0]) |
| 350 | debug("net_root_path : %s\n", net_root_path); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 351 | |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 352 | if (net_nis_domain[0]) |
| 353 | debug("net_nis_domain : %s\n", net_nis_domain); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 354 | |
Luuk Paulussen | 6380e01 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 355 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
Chris Packham | 3a2ae81 | 2018-05-03 20:19:03 +1200 | [diff] [blame] | 356 | if (net_ntp_server.s_addr) |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 357 | debug("net_ntp_server : %pI4\n", &net_ntp_server); |
Luuk Paulussen | 6380e01 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 358 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 359 | } |
Simon Glass | ab068eb | 2011-06-13 16:13:12 -0700 | [diff] [blame] | 360 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 361 | /* |
| 362 | * Handle a BOOTP received packet. |
| 363 | */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 364 | static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 365 | unsigned src, unsigned len) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 366 | { |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 367 | struct bootp_hdr *bp; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 368 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 369 | debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n", |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 370 | src, dest, len, sizeof(struct bootp_hdr)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 371 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 372 | bp = (struct bootp_hdr *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 373 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 374 | /* Filter out pkts we don't want */ |
Stefan Brüns | a48cd93 | 2015-08-27 23:53:26 +0200 | [diff] [blame] | 375 | if (check_reply_packet(pkt, dest, src, len)) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 376 | return; |
| 377 | |
| 378 | /* |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 379 | * Got a good BOOTP reply. Copy the data into our variables. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 380 | */ |
Uri Mashiach | 4892d39 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 381 | #if defined(CONFIG_LED_STATUS) && defined(CONFIG_LED_STATUS_BOOT_ENABLE) |
| 382 | status_led_set(CONFIG_LED_STATUS_BOOT, CONFIG_LED_STATUS_OFF); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 383 | #endif |
| 384 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 385 | store_net_params(bp); /* Store net parameters from reply */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 386 | |
| 387 | /* Retrieve extended information (we must parse the vendor area) */ |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 388 | if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 389 | bootp_process_vendor((uchar *)&bp->bp_vend[4], len); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 390 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 391 | net_set_timeout_handler(0, (thand_f *)0); |
Simon Glass | 768cbf0 | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 392 | bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 393 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 394 | debug("Got good BOOTP\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 395 | |
Simon Glass | 5234ad1 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 396 | net_auto_load(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 397 | } |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 398 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * Timeout on BOOTP/DHCP request. |
| 402 | */ |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 403 | static void bootp_timeout_handler(void) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 404 | { |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 405 | ulong time_taken = get_timer(bootp_start); |
| 406 | |
Alexandre Messier | 1597132 | 2016-02-01 17:08:57 -0500 | [diff] [blame] | 407 | if (time_taken >= time_taken_max) { |
Joe Hershberger | 8ca7fa0 | 2012-05-23 07:59:19 +0000 | [diff] [blame] | 408 | #ifdef CONFIG_BOOTP_MAY_FAIL |
Joe Hershberger | 724cc6a | 2017-11-07 18:13:40 -0800 | [diff] [blame] | 409 | char *ethrotate; |
| 410 | |
| 411 | ethrotate = env_get("ethrotate"); |
| 412 | if ((ethrotate && strcmp(ethrotate, "no") == 0) || |
| 413 | net_restart_wrap) { |
| 414 | puts("\nRetry time exceeded\n"); |
| 415 | net_set_state(NETLOOP_FAIL); |
| 416 | } else |
Joe Hershberger | 8ca7fa0 | 2012-05-23 07:59:19 +0000 | [diff] [blame] | 417 | #endif |
Joe Hershberger | 724cc6a | 2017-11-07 18:13:40 -0800 | [diff] [blame] | 418 | { |
| 419 | puts("\nRetry time exceeded; starting again\n"); |
| 420 | net_start_again(); |
| 421 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 422 | } else { |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 423 | bootp_timeout *= 2; |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 424 | if (bootp_timeout > 2000) |
| 425 | bootp_timeout = 2000; |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 426 | net_set_timeout_handler(bootp_timeout, bootp_timeout_handler); |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 427 | bootp_request(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Ilya Yanok | 3087658 | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 431 | #define put_vci(e, str) \ |
| 432 | do { \ |
| 433 | size_t vci_strlen = strlen(str); \ |
| 434 | *e++ = 60; /* Vendor Class Identifier */ \ |
| 435 | *e++ = vci_strlen; \ |
| 436 | memcpy(e, str, vci_strlen); \ |
| 437 | e += vci_strlen; \ |
| 438 | } while (0) |
| 439 | |
Alexander Graf | ea73a04 | 2016-05-06 21:01:02 +0200 | [diff] [blame] | 440 | static u8 *add_vci(u8 *e) |
| 441 | { |
Alexander Graf | 3661aac | 2016-05-06 21:01:07 +0200 | [diff] [blame] | 442 | char *vci = NULL; |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 443 | char *env_vci = env_get("bootp_vci"); |
Alexander Graf | 3661aac | 2016-05-06 21:01:07 +0200 | [diff] [blame] | 444 | |
Alexander Graf | ea73a04 | 2016-05-06 21:01:02 +0200 | [diff] [blame] | 445 | #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING) |
Alexander Graf | 3661aac | 2016-05-06 21:01:07 +0200 | [diff] [blame] | 446 | vci = CONFIG_SPL_NET_VCI_STRING; |
Alexander Graf | ea73a04 | 2016-05-06 21:01:02 +0200 | [diff] [blame] | 447 | #elif defined(CONFIG_BOOTP_VCI_STRING) |
Alexander Graf | 3661aac | 2016-05-06 21:01:07 +0200 | [diff] [blame] | 448 | vci = CONFIG_BOOTP_VCI_STRING; |
Alexander Graf | ea73a04 | 2016-05-06 21:01:02 +0200 | [diff] [blame] | 449 | #endif |
| 450 | |
Alexander Graf | 3661aac | 2016-05-06 21:01:07 +0200 | [diff] [blame] | 451 | if (env_vci) |
| 452 | vci = env_vci; |
| 453 | |
| 454 | if (vci) |
| 455 | put_vci(e, vci); |
| 456 | |
Alexander Graf | ea73a04 | 2016-05-06 21:01:02 +0200 | [diff] [blame] | 457 | return e; |
| 458 | } |
| 459 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 460 | /* |
| 461 | * Initialize BOOTP extension fields in the request. |
| 462 | */ |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 463 | #if defined(CONFIG_CMD_DHCP) |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 464 | static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip, |
| 465 | struct in_addr requested_ip) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 466 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 467 | u8 *start = e; |
| 468 | u8 *cnt; |
Alexander Graf | 3d5fc4e | 2016-05-12 15:51:45 +0200 | [diff] [blame] | 469 | #ifdef CONFIG_LIB_UUID |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 470 | char *uuid; |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 471 | #endif |
Alexander Graf | 3d5fc4e | 2016-05-12 15:51:45 +0200 | [diff] [blame] | 472 | int clientarch = -1; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 473 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 474 | #if defined(CONFIG_BOOTP_VENDOREX) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 475 | u8 *x; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 476 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 477 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 478 | char *hostname; |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 479 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 480 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 481 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 482 | *e++ = 130; |
| 483 | *e++ = 83; |
| 484 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 485 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 486 | *e++ = 53; /* DHCP Message Type */ |
| 487 | *e++ = 1; |
| 488 | *e++ = message_type; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 489 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 490 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 491 | *e++ = 2; |
Joe Hershberger | ceba447 | 2012-05-23 07:58:14 +0000 | [diff] [blame] | 492 | *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8; |
| 493 | *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 494 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 495 | if (server_ip.s_addr) { |
| 496 | int tmp = ntohl(server_ip.s_addr); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 497 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 498 | *e++ = 54; /* ServerID */ |
| 499 | *e++ = 4; |
| 500 | *e++ = tmp >> 24; |
| 501 | *e++ = tmp >> 16; |
| 502 | *e++ = tmp >> 8; |
| 503 | *e++ = tmp & 0xff; |
| 504 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 505 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 506 | if (requested_ip.s_addr) { |
| 507 | int tmp = ntohl(requested_ip.s_addr); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 508 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 509 | *e++ = 50; /* Requested IP */ |
| 510 | *e++ = 4; |
| 511 | *e++ = tmp >> 24; |
| 512 | *e++ = tmp >> 16; |
| 513 | *e++ = tmp >> 8; |
| 514 | *e++ = tmp & 0xff; |
| 515 | } |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 516 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 517 | hostname = env_get("hostname"); |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 518 | if (hostname) { |
| 519 | int hostnamelen = strlen(hostname); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 520 | |
| 521 | *e++ = 12; /* Hostname */ |
| 522 | *e++ = hostnamelen; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 523 | memcpy(e, hostname, hostnamelen); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 524 | e += hostnamelen; |
| 525 | } |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 526 | #endif |
| 527 | |
Alexander Graf | 3d5fc4e | 2016-05-12 15:51:45 +0200 | [diff] [blame] | 528 | #ifdef CONFIG_BOOTP_PXE_CLIENTARCH |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 529 | clientarch = CONFIG_BOOTP_PXE_CLIENTARCH; |
Alexander Graf | 3d5fc4e | 2016-05-12 15:51:45 +0200 | [diff] [blame] | 530 | #endif |
| 531 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 532 | if (env_get("bootp_arch")) |
Simon Glass | 22c34c2 | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 533 | clientarch = env_get_ulong("bootp_arch", 16, clientarch); |
Alexander Graf | 3d5fc4e | 2016-05-12 15:51:45 +0200 | [diff] [blame] | 534 | |
| 535 | if (clientarch > 0) { |
| 536 | *e++ = 93; /* Client System Architecture */ |
| 537 | *e++ = 2; |
| 538 | *e++ = (clientarch >> 8) & 0xff; |
| 539 | *e++ = clientarch & 0xff; |
| 540 | } |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 541 | |
| 542 | *e++ = 94; /* Client Network Interface Identifier */ |
| 543 | *e++ = 3; |
| 544 | *e++ = 1; /* type field for UNDI */ |
| 545 | *e++ = 0; /* major revision */ |
| 546 | *e++ = 0; /* minor revision */ |
| 547 | |
Alexander Graf | 3d5fc4e | 2016-05-12 15:51:45 +0200 | [diff] [blame] | 548 | #ifdef CONFIG_LIB_UUID |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 549 | uuid = env_get("pxeuuid"); |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 550 | |
| 551 | if (uuid) { |
| 552 | if (uuid_str_valid(uuid)) { |
| 553 | *e++ = 97; /* Client Machine Identifier */ |
| 554 | *e++ = 17; |
| 555 | *e++ = 0; /* type 0 - UUID */ |
| 556 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 557 | uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD); |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 558 | e += 16; |
| 559 | } else { |
| 560 | printf("Invalid pxeuuid: %s\n", uuid); |
| 561 | } |
| 562 | } |
Ilya Yanok | 3087658 | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 563 | #endif |
Jason Hobbs | ea320281 | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 564 | |
Alexander Graf | ea73a04 | 2016-05-06 21:01:02 +0200 | [diff] [blame] | 565 | e = add_vci(e); |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 566 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 567 | #if defined(CONFIG_BOOTP_VENDOREX) |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 568 | x = dhcp_vendorex_prep(e); |
| 569 | if (x) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 570 | return x - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 571 | #endif |
| 572 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 573 | *e++ = 55; /* Parameter Request List */ |
| 574 | cnt = e++; /* Pointer to count of requested items */ |
| 575 | *cnt = 0; |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 576 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 577 | *e++ = 1; /* Subnet Mask */ |
| 578 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 579 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 580 | #if defined(CONFIG_BOOTP_TIMEOFFSET) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 581 | *e++ = 2; |
| 582 | *cnt += 1; |
| 583 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 584 | #if defined(CONFIG_BOOTP_GATEWAY) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 585 | *e++ = 3; /* Router Option */ |
| 586 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 587 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 588 | #if defined(CONFIG_BOOTP_DNS) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 589 | *e++ = 6; /* DNS Server(s) */ |
| 590 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 591 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 592 | #if defined(CONFIG_BOOTP_HOSTNAME) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 593 | *e++ = 12; /* Hostname */ |
| 594 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 595 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 596 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 597 | *e++ = 13; /* Boot File Size */ |
| 598 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 599 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 600 | #if defined(CONFIG_BOOTP_BOOTPATH) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 601 | *e++ = 17; /* Boot path */ |
| 602 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 603 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 604 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 605 | *e++ = 40; /* NIS Domain name request */ |
| 606 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 607 | #endif |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 608 | #if defined(CONFIG_BOOTP_NTPSERVER) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 609 | *e++ = 42; |
| 610 | *cnt += 1; |
| 611 | #endif |
Jason Liu | 1c7dca5 | 2010-11-14 12:23:09 +0800 | [diff] [blame] | 612 | /* no options, so back up to avoid sending an empty request list */ |
| 613 | if (*cnt == 0) |
| 614 | e -= 2; |
| 615 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 616 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 617 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 618 | /* Pad to minimal length */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 619 | #ifdef CONFIG_DHCP_MIN_EXT_LEN |
Simon Glass | a0978eb | 2011-02-02 15:03:28 -0800 | [diff] [blame] | 620 | while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 621 | *e++ = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 622 | #endif |
| 623 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 624 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 627 | #else |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 628 | /* |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 629 | * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 630 | */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 631 | static int bootp_extended(u8 *e) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 632 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 633 | u8 *start = e; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 634 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 635 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 636 | *e++ = 130; |
| 637 | *e++ = 83; |
| 638 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 639 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 640 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 641 | *e++ = 53; /* DHCP Message Type */ |
| 642 | *e++ = 1; |
| 643 | *e++ = DHCP_DISCOVER; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 644 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 645 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 646 | *e++ = 2; |
Joe Hershberger | ceba447 | 2012-05-23 07:58:14 +0000 | [diff] [blame] | 647 | *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16; |
| 648 | *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff; |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 649 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 650 | |
Walter Stoll | 8182a3a | 2021-10-12 11:01:59 +0000 | [diff] [blame] | 651 | e = add_vci(e); |
Ilya Yanok | 3087658 | 2012-09-17 10:26:25 +0000 | [diff] [blame] | 652 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 653 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 654 | *e++ = 1; /* Subnet mask request */ |
| 655 | *e++ = 4; |
| 656 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 657 | #endif |
| 658 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 659 | #if defined(CONFIG_BOOTP_GATEWAY) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 660 | *e++ = 3; /* Default gateway request */ |
| 661 | *e++ = 4; |
| 662 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 663 | #endif |
| 664 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 665 | #if defined(CONFIG_BOOTP_DNS) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 666 | *e++ = 6; /* Domain Name Server */ |
| 667 | *e++ = 4; |
| 668 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 669 | #endif |
| 670 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 671 | #if defined(CONFIG_BOOTP_HOSTNAME) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 672 | *e++ = 12; /* Host name request */ |
| 673 | *e++ = 32; |
| 674 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 675 | #endif |
| 676 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 677 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 678 | *e++ = 13; /* Boot file size */ |
| 679 | *e++ = 2; |
| 680 | e += 2; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 681 | #endif |
| 682 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 683 | #if defined(CONFIG_BOOTP_BOOTPATH) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 684 | *e++ = 17; /* Boot path */ |
| 685 | *e++ = 32; |
| 686 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 687 | #endif |
| 688 | |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 689 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 690 | *e++ = 40; /* NIS Domain name request */ |
| 691 | *e++ = 32; |
| 692 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 693 | #endif |
Luuk Paulussen | 6380e01 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 694 | #if defined(CONFIG_BOOTP_NTPSERVER) |
| 695 | *e++ = 42; |
| 696 | *e++ = 4; |
| 697 | e += 4; |
| 698 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 699 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 700 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 701 | |
Andre Renaud | 23298cd | 2016-05-05 07:28:08 -0600 | [diff] [blame] | 702 | /* |
| 703 | * If nothing in list, remove it altogether. Some DHCP servers get |
| 704 | * upset by this minor faux pas and do not respond at all. |
| 705 | */ |
| 706 | if (e == start + 3) { |
| 707 | printf("*** Warning: no DHCP options requested\n"); |
| 708 | e -= 3; |
| 709 | } |
| 710 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 711 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 712 | } |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 713 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 714 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 715 | void bootp_reset(void) |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 716 | { |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 717 | bootp_num_ids = 0; |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 718 | bootp_try = 0; |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 719 | bootp_start = get_timer(0); |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 720 | bootp_timeout = 250; |
Stephen Warren | 69e1e35 | 2014-07-25 17:30:48 -0600 | [diff] [blame] | 721 | } |
| 722 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 723 | void bootp_request(void) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 724 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 725 | uchar *pkt, *iphdr; |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 726 | struct bootp_hdr *bp; |
Joe Hershberger | db3e6e4 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 727 | int extlen, pktlen, iplen; |
| 728 | int eth_hdr_size; |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 729 | #ifdef CONFIG_BOOTP_RANDOM_DELAY |
Pavel Machek | 764c29d | 2014-07-11 11:39:37 +0200 | [diff] [blame] | 730 | ulong rand_ms; |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 731 | #endif |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 732 | u32 bootp_id; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 733 | struct in_addr zero_ip; |
| 734 | struct in_addr bcast_ip; |
Alexandre Messier | 1597132 | 2016-02-01 17:08:57 -0500 | [diff] [blame] | 735 | char *ep; /* Environment pointer */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 736 | |
Simon Glass | 768cbf0 | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 737 | bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start"); |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 738 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 739 | dhcp_state = INIT; |
| 740 | #endif |
| 741 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 742 | ep = env_get("bootpretryperiod"); |
Alexandre Messier | 1597132 | 2016-02-01 17:08:57 -0500 | [diff] [blame] | 743 | if (ep != NULL) |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 744 | time_taken_max = dectoul(ep, NULL); |
Alexandre Messier | 1597132 | 2016-02-01 17:08:57 -0500 | [diff] [blame] | 745 | else |
| 746 | time_taken_max = TIMEOUT_MS; |
| 747 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 748 | #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 749 | if (bootp_try == 0) |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 750 | srand_mac(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 751 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 752 | if (bootp_try <= 2) /* Start with max 1024 * 1ms */ |
| 753 | rand_ms = rand() >> (22 - bootp_try); |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 754 | else /* After 3rd BOOTP request max 8192 * 1ms */ |
| 755 | rand_ms = rand() >> 19; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 756 | |
Joe Hershberger | 797f2c5 | 2012-05-23 07:57:58 +0000 | [diff] [blame] | 757 | printf("Random delay: %ld ms...\n", rand_ms); |
Pavel Machek | 764c29d | 2014-07-11 11:39:37 +0200 | [diff] [blame] | 758 | mdelay(rand_ms); |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 759 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 760 | #endif /* CONFIG_BOOTP_RANDOM_DELAY */ |
| 761 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 762 | printf("BOOTP broadcast %d\n", ++bootp_try); |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 763 | pkt = net_tx_packet; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 764 | memset((void *)pkt, 0, PKTSIZE); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 765 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 766 | eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP); |
Joe Hershberger | db3e6e4 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 767 | pkt += eth_hdr_size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 768 | |
| 769 | /* |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 770 | * Next line results in incorrect packet size being transmitted, |
| 771 | * resulting in errors in some DHCP servers, reporting missing bytes. |
| 772 | * Size must be set in packet header after extension length has been |
| 773 | * determined. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 774 | * C. Hallinan, DS4.COM, Inc. |
| 775 | */ |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 776 | /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 777 | sizeof (struct bootp_hdr)); */ |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 778 | iphdr = pkt; /* We need this later for net_set_udp_header() */ |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 779 | pkt += IP_UDP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 780 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 781 | bp = (struct bootp_hdr *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 782 | bp->bp_op = OP_BOOTREQUEST; |
| 783 | bp->bp_htype = HWT_ETHER; |
| 784 | bp->bp_hlen = HWL_ETHER; |
| 785 | bp->bp_hops = 0; |
Stefan Brüns | 2eb2556 | 2015-08-27 23:57:18 +0200 | [diff] [blame] | 786 | /* |
| 787 | * according to RFC1542, should be 0 on first request, secs since |
| 788 | * first request otherwise |
| 789 | */ |
| 790 | bp->bp_secs = htons(get_timer(bootp_start) / 1000); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 791 | zero_ip.s_addr = 0; |
| 792 | net_write_ip(&bp->bp_ciaddr, zero_ip); |
| 793 | net_write_ip(&bp->bp_yiaddr, zero_ip); |
| 794 | net_write_ip(&bp->bp_siaddr, zero_ip); |
| 795 | net_write_ip(&bp->bp_giaddr, zero_ip); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 796 | memcpy(bp->bp_chaddr, net_ethaddr, 6); |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 797 | copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 798 | |
| 799 | /* Request additional information from the BOOTP/DHCP server */ |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 800 | #if defined(CONFIG_CMD_DHCP) |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 801 | extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip, |
| 802 | zero_ip); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 803 | #else |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 804 | extlen = bootp_extended((u8 *)bp->bp_vend); |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 805 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * Bootp ID is the lower 4 bytes of our ethernet address |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 809 | * plus the current time in ms. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 810 | */ |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 811 | bootp_id = ((u32)net_ethaddr[2] << 24) |
| 812 | | ((u32)net_ethaddr[3] << 16) |
| 813 | | ((u32)net_ethaddr[4] << 8) |
| 814 | | (u32)net_ethaddr[5]; |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 815 | bootp_id += get_timer(0); |
| 816 | bootp_id = htonl(bootp_id); |
| 817 | bootp_add_id(bootp_id); |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 818 | net_copy_u32(&bp->bp_id, &bootp_id); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 819 | |
| 820 | /* |
| 821 | * Calculate proper packet lengths taking into account the |
| 822 | * variable size of the options field |
| 823 | */ |
Joe Hershberger | db3e6e4 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 824 | iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; |
| 825 | pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 826 | bcast_ip.s_addr = 0xFFFFFFFFL; |
| 827 | net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 828 | net_set_timeout_handler(bootp_timeout, bootp_timeout_handler); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 829 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 830 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 831 | dhcp_state = SELECTING; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 832 | net_set_udp_handler(dhcp_handler); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 833 | #else |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 834 | net_set_udp_handler(bootp_handler); |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 835 | #endif |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 836 | net_send_packet(net_tx_packet, pktlen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 839 | #if defined(CONFIG_CMD_DHCP) |
Stefan Brüns | 79f724c | 2015-09-04 00:31:49 +0200 | [diff] [blame] | 840 | static void dhcp_process_options(uchar *popt, uchar *end) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 841 | { |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 842 | int oplen, size; |
Wolfgang Denk | 2b18422 | 2009-09-11 09:05:32 +0200 | [diff] [blame] | 843 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
| 844 | int *to_ptr; |
| 845 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 846 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 847 | while (popt < end && *popt != 0xff) { |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 848 | oplen = *(popt + 1); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 849 | switch (*popt) { |
Stefan Brüns | 4431829 | 2015-08-28 10:15:54 +0200 | [diff] [blame] | 850 | case 0: |
| 851 | oplen = -1; /* Pad omits len byte */ |
| 852 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 853 | case 1: |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 854 | net_copy_ip(&net_netmask, (popt + 2)); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 855 | break; |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 856 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 857 | case 2: /* Time offset */ |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 858 | to_ptr = &net_ntp_time_offset; |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 859 | net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2)); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 860 | net_ntp_time_offset = ntohl(net_ntp_time_offset); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 861 | break; |
| 862 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 863 | case 3: |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 864 | net_copy_ip(&net_gateway, (popt + 2)); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 865 | break; |
| 866 | case 6: |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 867 | net_copy_ip(&net_dns_server, (popt + 2)); |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 868 | #if defined(CONFIG_BOOTP_DNS2) |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 869 | if (*(popt + 1) > 4) |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 870 | net_copy_ip(&net_dns_server2, (popt + 2 + 4)); |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 871 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 872 | break; |
| 873 | case 12: |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 874 | size = truncate_sz("Host Name", |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 875 | sizeof(net_hostname), oplen); |
| 876 | memcpy(&net_hostname, popt + 2, size); |
| 877 | net_hostname[size] = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 878 | break; |
| 879 | case 15: /* Ignore Domain Name Option */ |
| 880 | break; |
| 881 | case 17: |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 882 | size = truncate_sz("Root Path", |
Joe Hershberger | 6d23643 | 2015-04-08 01:41:03 -0500 | [diff] [blame] | 883 | sizeof(net_root_path), oplen); |
| 884 | memcpy(&net_root_path, popt + 2, size); |
| 885 | net_root_path[size] = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 886 | break; |
Brian Rzycki | 5a672a2 | 2012-09-11 09:22:53 +0000 | [diff] [blame] | 887 | case 28: /* Ignore Broadcast Address Option */ |
| 888 | break; |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 889 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 890 | case 42: /* NTP server IP */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 891 | net_copy_ip(&net_ntp_server, (popt + 2)); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 892 | break; |
| 893 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 894 | case 51: |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 895 | net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2)); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 896 | break; |
Stefan Brüns | 345cb83 | 2015-09-04 00:31:48 +0200 | [diff] [blame] | 897 | case 52: |
| 898 | dhcp_option_overload = popt[2]; |
| 899 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 900 | case 53: /* Ignore Message Type Option */ |
| 901 | break; |
| 902 | case 54: |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 903 | net_copy_ip(&dhcp_server_ip, (popt + 2)); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 904 | break; |
| 905 | case 58: /* Ignore Renewal Time Option */ |
| 906 | break; |
| 907 | case 59: /* Ignore Rebinding Time Option */ |
| 908 | break; |
Wolfgang Denk | 012429c | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 909 | case 66: /* Ignore TFTP server name */ |
| 910 | break; |
Stefan Brüns | 345cb83 | 2015-09-04 00:31:48 +0200 | [diff] [blame] | 911 | case 67: /* Bootfile option */ |
Alexander Graf | f43bf5d | 2018-06-15 10:29:27 +0200 | [diff] [blame] | 912 | if (!net_boot_file_name_explicit) { |
| 913 | size = truncate_sz("Bootfile", |
| 914 | sizeof(net_boot_file_name), |
| 915 | oplen); |
| 916 | memcpy(&net_boot_file_name, popt + 2, size); |
| 917 | net_boot_file_name[size] = 0; |
| 918 | } |
Wolfgang Denk | 012429c | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 919 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 920 | default: |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 921 | #if defined(CONFIG_BOOTP_VENDOREX) |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 922 | if (dhcp_vendorex_proc(popt)) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 923 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 924 | #endif |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 925 | printf("*** Unhandled DHCP Option in OFFER/ACK:" |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 926 | " %d\n", *popt); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 927 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 928 | } |
| 929 | popt += oplen + 2; /* Process next option */ |
| 930 | } |
| 931 | } |
| 932 | |
Stefan Brüns | 79f724c | 2015-09-04 00:31:49 +0200 | [diff] [blame] | 933 | static void dhcp_packet_process_options(struct bootp_hdr *bp) |
| 934 | { |
| 935 | uchar *popt = (uchar *)&bp->bp_vend[4]; |
| 936 | uchar *end = popt + BOOTP_HDR_SIZE; |
| 937 | |
| 938 | if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC)) |
| 939 | return; |
| 940 | |
| 941 | dhcp_option_overload = 0; |
| 942 | |
| 943 | /* |
| 944 | * The 'options' field MUST be interpreted first, 'file' next, |
| 945 | * 'sname' last. |
| 946 | */ |
| 947 | dhcp_process_options(popt, end); |
| 948 | |
| 949 | if (dhcp_option_overload & OVERLOAD_FILE) { |
| 950 | popt = (uchar *)bp->bp_file; |
| 951 | end = popt + sizeof(bp->bp_file); |
| 952 | dhcp_process_options(popt, end); |
| 953 | } |
| 954 | |
| 955 | if (dhcp_option_overload & OVERLOAD_SNAME) { |
| 956 | popt = (uchar *)bp->bp_sname; |
| 957 | end = popt + sizeof(bp->bp_sname); |
| 958 | dhcp_process_options(popt, end); |
| 959 | } |
| 960 | } |
| 961 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 962 | static int dhcp_message_type(unsigned char *popt) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 963 | { |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 964 | if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC)) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 965 | return -1; |
| 966 | |
| 967 | popt += 4; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 968 | while (*popt != 0xff) { |
| 969 | if (*popt == 53) /* DHCP Message Type */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 970 | return *(popt + 2); |
Stefan Brüns | 4431829 | 2015-08-28 10:15:54 +0200 | [diff] [blame] | 971 | if (*popt == 0) { |
| 972 | /* Pad */ |
| 973 | popt += 1; |
| 974 | } else { |
| 975 | /* Scan through all options */ |
| 976 | popt += *(popt + 1) + 2; |
| 977 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 978 | } |
| 979 | return -1; |
| 980 | } |
| 981 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 982 | static void dhcp_send_request_packet(struct bootp_hdr *bp_offer) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 983 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 984 | uchar *pkt, *iphdr; |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 985 | struct bootp_hdr *bp; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 986 | int pktlen, iplen, extlen; |
Joe Hershberger | db3e6e4 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 987 | int eth_hdr_size; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 988 | struct in_addr offered_ip; |
| 989 | struct in_addr zero_ip; |
| 990 | struct in_addr bcast_ip; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 991 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 992 | debug("dhcp_send_request_packet: Sending DHCPREQUEST\n"); |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 993 | pkt = net_tx_packet; |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 994 | memset((void *)pkt, 0, PKTSIZE); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 995 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 996 | eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP); |
Joe Hershberger | db3e6e4 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 997 | pkt += eth_hdr_size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 998 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 999 | iphdr = pkt; /* We'll need this later to set proper pkt size */ |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 1000 | pkt += IP_UDP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1001 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1002 | bp = (struct bootp_hdr *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1003 | bp->bp_op = OP_BOOTREQUEST; |
| 1004 | bp->bp_htype = HWT_ETHER; |
| 1005 | bp->bp_hlen = HWL_ETHER; |
| 1006 | bp->bp_hops = 0; |
Stefan Brüns | 2eb2556 | 2015-08-27 23:57:18 +0200 | [diff] [blame] | 1007 | bp->bp_secs = htons(get_timer(bootp_start) / 1000); |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1008 | /* Do not set the client IP, your IP, or server IP yet, since it |
| 1009 | * hasn't been ACK'ed by the server yet */ |
Justin Flammia | 01f256b | 2007-10-29 17:40:35 -0400 | [diff] [blame] | 1010 | |
Wolfgang Denk | 8a933fb | 2006-10-12 00:01:08 +0200 | [diff] [blame] | 1011 | /* |
Wolfgang Denk | 68e83a8 | 2006-10-09 01:26:14 +0200 | [diff] [blame] | 1012 | * RFC3046 requires Relay Agents to discard packets with |
| 1013 | * nonzero and offered giaddr |
| 1014 | */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 1015 | zero_ip.s_addr = 0; |
| 1016 | net_write_ip(&bp->bp_giaddr, zero_ip); |
Wolfgang Denk | 68e83a8 | 2006-10-09 01:26:14 +0200 | [diff] [blame] | 1017 | |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1018 | memcpy(bp->bp_chaddr, net_ethaddr, 6); |
Alexandre Messier | ad28a31 | 2016-01-28 11:19:02 -0500 | [diff] [blame] | 1019 | copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1020 | |
| 1021 | /* |
| 1022 | * ID is the id of the OFFER packet |
| 1023 | */ |
| 1024 | |
Sergey Temerkhanov | bf5ad64 | 2015-04-08 01:41:22 -0500 | [diff] [blame] | 1025 | net_copy_u32(&bp->bp_id, &bp_offer->bp_id); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1026 | |
| 1027 | /* |
| 1028 | * Copy options from OFFER packet if present |
| 1029 | */ |
Justin Flammia | 01f256b | 2007-10-29 17:40:35 -0400 | [diff] [blame] | 1030 | |
| 1031 | /* Copy offered IP into the parameters request list */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 1032 | net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr); |
| 1033 | extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST, |
| 1034 | dhcp_server_ip, offered_ip); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1035 | |
Joe Hershberger | db3e6e4 | 2012-05-23 07:59:10 +0000 | [diff] [blame] | 1036 | iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen; |
| 1037 | pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 1038 | bcast_ip.s_addr = 0xFFFFFFFFL; |
| 1039 | net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1040 | |
Joe Hershberger | b1e9476 | 2012-05-23 07:59:11 +0000 | [diff] [blame] | 1041 | debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 1042 | net_send_packet(net_tx_packet, pktlen); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Handle DHCP received packets. |
| 1047 | */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 1048 | static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 1049 | unsigned src, unsigned len) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1050 | { |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1051 | struct bootp_hdr *bp = (struct bootp_hdr *)pkt; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1052 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1053 | debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1054 | src, dest, len, dhcp_state); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1055 | |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1056 | /* Filter out pkts we don't want */ |
Stefan Brüns | a48cd93 | 2015-08-27 23:53:26 +0200 | [diff] [blame] | 1057 | if (check_reply_packet(pkt, dest, src, len)) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1058 | return; |
| 1059 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1060 | debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: " |
| 1061 | "%d\n", src, dest, len, dhcp_state); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1062 | |
Lyle Franklin | 73fcbc7 | 2019-08-05 06:23:42 -0400 | [diff] [blame] | 1063 | if (net_read_ip(&bp->bp_yiaddr).s_addr == 0) { |
| 1064 | #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP) |
| 1065 | store_bootp_params(bp); |
| 1066 | #endif |
Peng Fan | 67e72e9 | 2016-01-07 15:28:23 +0800 | [diff] [blame] | 1067 | return; |
Lyle Franklin | 73fcbc7 | 2019-08-05 06:23:42 -0400 | [diff] [blame] | 1068 | } |
Peng Fan | 67e72e9 | 2016-01-07 15:28:23 +0800 | [diff] [blame] | 1069 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1070 | switch (dhcp_state) { |
| 1071 | case SELECTING: |
| 1072 | /* |
| 1073 | * Wait an appropriate time for any potential DHCPOFFER packets |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1074 | * to arrive. Then select one, and generate DHCPREQUEST |
| 1075 | * response. If filename is in format we recognize, assume it |
| 1076 | * is a valid OFFER from a server we want. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1077 | */ |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1078 | debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1079 | #ifdef CONFIG_SYS_BOOTFILE_PREFIX |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1080 | if (strncmp(bp->bp_file, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1081 | CONFIG_SYS_BOOTFILE_PREFIX, |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1082 | strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1083 | #endif /* CONFIG_SYS_BOOTFILE_PREFIX */ |
Stefan Brüns | 79f724c | 2015-09-04 00:31:49 +0200 | [diff] [blame] | 1084 | dhcp_packet_process_options(bp); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1085 | efi_net_set_dhcp_ack(pkt, len); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1086 | |
Lyle Franklin | 73fcbc7 | 2019-08-05 06:23:42 -0400 | [diff] [blame] | 1087 | #if defined(CONFIG_SERVERIP_FROM_PROXYDHCP) |
| 1088 | if (!net_server_ip.s_addr) |
| 1089 | udelay(CONFIG_SERVERIP_FROM_PROXYDHCP_DELAY_MS * |
| 1090 | 1000); |
| 1091 | #endif /* CONFIG_SERVERIP_FROM_PROXYDHCP */ |
| 1092 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1093 | debug("TRANSITIONING TO REQUESTING STATE\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1094 | dhcp_state = REQUESTING; |
stroese | 5fa6e90 | 2003-04-10 13:26:44 +0000 | [diff] [blame] | 1095 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 1096 | net_set_timeout_handler(5000, bootp_timeout_handler); |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1097 | dhcp_send_request_packet(bp); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1098 | #ifdef CONFIG_SYS_BOOTFILE_PREFIX |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1099 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1100 | #endif /* CONFIG_SYS_BOOTFILE_PREFIX */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1101 | |
| 1102 | return; |
| 1103 | break; |
| 1104 | case REQUESTING: |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1105 | debug("DHCP State: REQUESTING\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1106 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1107 | if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) { |
Stefan Brüns | 79f724c | 2015-09-04 00:31:49 +0200 | [diff] [blame] | 1108 | dhcp_packet_process_options(bp); |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1109 | /* Store net params from reply */ |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1110 | store_net_params(bp); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1111 | dhcp_state = BOUND; |
Thierry Reding | 8977cda | 2014-08-19 10:21:24 +0200 | [diff] [blame] | 1112 | printf("DHCP client bound to address %pI4 (%lu ms)\n", |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1113 | &net_ip, get_timer(bootp_start)); |
Stefan Brüns | 4c60e4a | 2015-08-30 17:47:17 +0200 | [diff] [blame] | 1114 | net_set_timeout_handler(0, (thand_f *)0); |
Simon Glass | 768cbf0 | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 1115 | bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1116 | "bootp_stop"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1117 | |
Simon Glass | 5234ad1 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 1118 | net_auto_load(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1119 | return; |
| 1120 | } |
| 1121 | break; |
Remy Bohmer | 03a4449 | 2008-08-20 11:30:28 +0200 | [diff] [blame] | 1122 | case BOUND: |
| 1123 | /* DHCP client bound to address */ |
| 1124 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1125 | default: |
Joe Hershberger | 8f4b135 | 2012-05-15 08:59:06 +0000 | [diff] [blame] | 1126 | puts("DHCP: INVALID STATE\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1127 | break; |
| 1128 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1131 | void dhcp_request(void) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1132 | { |
Joe Hershberger | 2fe81a5 | 2015-04-08 01:41:09 -0500 | [diff] [blame] | 1133 | bootp_request(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1134 | } |
Wolfgang Denk | f7a7f08 | 2007-11-03 23:09:27 +0100 | [diff] [blame] | 1135 | #endif /* CONFIG_CMD_DHCP */ |