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