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