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