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