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