]>
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 | ||
11 | #if 0 | |
232c150a WD |
12 | #define DEBUG 1 /* general debug */ |
13 | #define DEBUG_BOOTP_EXT 1 /* Debug received vendor fields */ | |
3861aa5c WD |
14 | #endif |
15 | ||
16 | #ifdef DEBUG_BOOTP_EXT | |
17 | #define debug_ext(fmt,args...) printf (fmt ,##args) | |
18 | #else | |
19 | #define debug_ext(fmt,args...) | |
20 | #endif | |
21 | ||
22 | #include <common.h> | |
23 | #include <command.h> | |
24 | #include <net.h> | |
25 | #include "bootp.h" | |
26 | #include "tftp.h" | |
232c150a | 27 | #include "nfs.h" |
3861aa5c WD |
28 | #ifdef CONFIG_STATUS_LED |
29 | #include <status_led.h> | |
30 | #endif | |
31 | ||
232c150a | 32 | #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ |
3861aa5c | 33 | |
643d1ab2 | 34 | #if defined(CONFIG_CMD_NET) |
3861aa5c WD |
35 | |
36 | #define TIMEOUT 5 /* Seconds before trying BOOTP again */ | |
232c150a | 37 | #ifndef CONFIG_NET_RETRY_COUNT |
3861aa5c WD |
38 | # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
39 | #else | |
232c150a | 40 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
3861aa5c WD |
41 | #endif |
42 | ||
43 | #define PORT_BOOTPS 67 /* BOOTP server UDP port */ | |
44 | #define PORT_BOOTPC 68 /* BOOTP client UDP port */ | |
45 | ||
46 | #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ | |
232c150a | 47 | #define CONFIG_DHCP_MIN_EXT_LEN 64 |
3861aa5c WD |
48 | #endif |
49 | ||
50 | ulong BootpID; | |
51 | int BootpTry; | |
52 | #ifdef CONFIG_BOOTP_RANDOM_DELAY | |
53 | ulong seed1, seed2; | |
54 | #endif | |
55 | ||
643d1ab2 | 56 | #if defined(CONFIG_CMD_DHCP) |
3861aa5c | 57 | dhcp_state_t dhcp_state = INIT; |
682011ff | 58 | unsigned long dhcp_leasetime = 0; |
759a51b4 | 59 | IPaddr_t NetDHCPServerIP = 0; |
3861aa5c WD |
60 | static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len); |
61 | ||
62 | /* For Debug */ | |
3e38691e WD |
63 | #if 0 |
64 | static char *dhcpmsg2str(int type) | |
3861aa5c WD |
65 | { |
66 | switch (type) { | |
232c150a WD |
67 | case 1: return "DHCPDISCOVER"; break; |
68 | case 2: return "DHCPOFFER"; break; | |
69 | case 3: return "DHCPREQUEST"; break; | |
70 | case 4: return "DHCPDECLINE"; break; | |
71 | case 5: return "DHCPACK"; break; | |
72 | case 6: return "DHCPNACK"; break; | |
73 | case 7: return "DHCPRELEASE"; break; | |
3861aa5c WD |
74 | default: return "UNKNOWN/INVALID MSG TYPE"; break; |
75 | } | |
76 | } | |
3e38691e | 77 | #endif |
3861aa5c | 78 | |
1fe80d79 | 79 | #if defined(CONFIG_BOOTP_VENDOREX) |
3861aa5c WD |
80 | extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */ |
81 | extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */ | |
82 | #endif | |
83 | ||
610f2e9c | 84 | #endif |
3861aa5c WD |
85 | |
86 | static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) | |
87 | { | |
88 | Bootp_t *bp = (Bootp_t *) pkt; | |
89 | int retval = 0; | |
90 | ||
91 | if (dest != PORT_BOOTPC || src != PORT_BOOTPS) | |
92 | retval = -1; | |
93 | else if (len < sizeof (Bootp_t) - OPT_SIZE) | |
94 | retval = -2; | |
95 | else if (bp->bp_op != OP_BOOTREQUEST && | |
96 | bp->bp_op != OP_BOOTREPLY && | |
97 | bp->bp_op != DHCP_OFFER && | |
98 | bp->bp_op != DHCP_ACK && | |
99 | bp->bp_op != DHCP_NAK ) { | |
100 | retval = -3; | |
101 | } | |
102 | else if (bp->bp_htype != HWT_ETHER) | |
103 | retval = -4; | |
104 | else if (bp->bp_hlen != HWL_ETHER) | |
105 | retval = -5; | |
106 | else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) { | |
107 | retval = -6; | |
108 | } | |
109 | ||
110 | debug ("Filtering pkt = %d\n", retval); | |
111 | ||
112 | return retval; | |
113 | } | |
114 | ||
115 | /* | |
116 | * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet | |
117 | */ | |
3e38691e | 118 | static void BootpCopyNetParams(Bootp_t *bp) |
3861aa5c | 119 | { |
3d3befa7 WD |
120 | IPaddr_t tmp_ip; |
121 | ||
3861aa5c | 122 | NetCopyIP(&NetOurIP, &bp->bp_yiaddr); |
5d110f0a | 123 | #if !defined(CONFIG_BOOTP_SERVERIP) |
3d3befa7 WD |
124 | NetCopyIP(&tmp_ip, &bp->bp_siaddr); |
125 | if (tmp_ip != 0) | |
126 | NetCopyIP(&NetServerIP, &bp->bp_siaddr); | |
3861aa5c | 127 | memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6); |
5d110f0a | 128 | #endif |
3d3befa7 WD |
129 | if (strlen(bp->bp_file) > 0) |
130 | copy_filename (BootFile, bp->bp_file, sizeof(BootFile)); | |
3861aa5c WD |
131 | |
132 | debug ("Bootfile: %s\n", BootFile); | |
133 | ||
134 | /* Propagate to environment: | |
8bde7f77 | 135 | * don't delete exising entry when BOOTP / DHCP reply does |
3861aa5c WD |
136 | * not contain a new value |
137 | */ | |
138 | if (*BootFile) { | |
139 | setenv ("bootfile", BootFile); | |
140 | } | |
141 | } | |
142 | ||
143 | static int truncate_sz (const char *name, int maxlen, int curlen) | |
144 | { | |
145 | if (curlen >= maxlen) { | |
146 | printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n", | |
147 | name, curlen, maxlen); | |
148 | curlen = maxlen - 1; | |
149 | } | |
150 | return (curlen); | |
151 | } | |
152 | ||
643d1ab2 | 153 | #if !defined(CONFIG_CMD_DHCP) |
3861aa5c | 154 | |
232c150a | 155 | static void BootpVendorFieldProcess (u8 * ext) |
3861aa5c | 156 | { |
232c150a | 157 | int size = *(ext + 1); |
3861aa5c | 158 | |
232c150a WD |
159 | debug_ext ("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, |
160 | *(ext + 1)); | |
3861aa5c | 161 | |
232c150a | 162 | NetBootFileSize = 0; |
3861aa5c | 163 | |
232c150a WD |
164 | switch (*ext) { |
165 | /* Fixed length fields */ | |
166 | case 1: /* Subnet mask */ | |
3861aa5c | 167 | if (NetOurSubnetMask == 0) |
232c150a | 168 | NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); |
3861aa5c | 169 | break; |
232c150a | 170 | case 2: /* Time offset - Not yet supported */ |
3861aa5c | 171 | break; |
232c150a WD |
172 | /* Variable length fields */ |
173 | case 3: /* Gateways list */ | |
3861aa5c | 174 | if (NetOurGatewayIP == 0) { |
232c150a | 175 | NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); |
3861aa5c WD |
176 | } |
177 | break; | |
232c150a | 178 | case 4: /* Time server - Not yet supported */ |
3861aa5c | 179 | break; |
232c150a | 180 | case 5: /* IEN-116 name server - Not yet supported */ |
3861aa5c WD |
181 | break; |
182 | case 6: | |
183 | if (NetOurDNSIP == 0) { | |
232c150a | 184 | NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2)); |
3861aa5c | 185 | } |
1fe80d79 | 186 | #if defined(CONFIG_BOOTP_DNS2) |
fe389a82 | 187 | if ((NetOurDNS2IP == 0) && (size > 4)) { |
232c150a | 188 | NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); |
fe389a82 SR |
189 | } |
190 | #endif | |
3861aa5c | 191 | break; |
232c150a | 192 | case 7: /* Log server - Not yet supported */ |
3861aa5c | 193 | break; |
232c150a | 194 | case 8: /* Cookie/Quote server - Not yet supported */ |
3861aa5c | 195 | break; |
232c150a | 196 | case 9: /* LPR server - Not yet supported */ |
3861aa5c | 197 | break; |
232c150a | 198 | case 10: /* Impress server - Not yet supported */ |
3861aa5c | 199 | break; |
232c150a | 200 | case 11: /* RPL server - Not yet supported */ |
3861aa5c | 201 | break; |
232c150a | 202 | case 12: /* Host name */ |
3861aa5c | 203 | if (NetOurHostName[0] == 0) { |
232c150a WD |
204 | size = truncate_sz ("Host Name", sizeof (NetOurHostName), size); |
205 | memcpy (&NetOurHostName, ext + 2, size); | |
206 | NetOurHostName[size] = 0; | |
3861aa5c WD |
207 | } |
208 | break; | |
232c150a | 209 | case 13: /* Boot file size */ |
3861aa5c | 210 | if (size == 2) |
232c150a | 211 | NetBootFileSize = ntohs (*(ushort *) (ext + 2)); |
3861aa5c | 212 | else if (size == 4) |
232c150a | 213 | NetBootFileSize = ntohl (*(ulong *) (ext + 2)); |
3861aa5c | 214 | break; |
232c150a | 215 | case 14: /* Merit dump file - Not yet supported */ |
3861aa5c | 216 | break; |
232c150a | 217 | case 15: /* Domain name - Not yet supported */ |
3861aa5c | 218 | break; |
232c150a | 219 | case 16: /* Swap server - Not yet supported */ |
3861aa5c | 220 | break; |
232c150a | 221 | case 17: /* Root path */ |
3861aa5c | 222 | if (NetOurRootPath[0] == 0) { |
232c150a WD |
223 | size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size); |
224 | memcpy (&NetOurRootPath, ext + 2, size); | |
225 | NetOurRootPath[size] = 0; | |
3861aa5c WD |
226 | } |
227 | break; | |
232c150a | 228 | case 18: /* Extension path - Not yet supported */ |
3861aa5c | 229 | /* |
8bde7f77 WD |
230 | * This can be used to send the information of the |
231 | * vendor area in another file that the client can | |
232 | * access via TFTP. | |
3861aa5c WD |
233 | */ |
234 | break; | |
232c150a WD |
235 | /* IP host layer fields */ |
236 | case 40: /* NIS Domain name */ | |
3861aa5c | 237 | if (NetOurNISDomain[0] == 0) { |
232c150a WD |
238 | size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size); |
239 | memcpy (&NetOurNISDomain, ext + 2, size); | |
240 | NetOurNISDomain[size] = 0; | |
3861aa5c WD |
241 | } |
242 | break; | |
232c150a WD |
243 | /* Application layer fields */ |
244 | case 43: /* Vendor specific info - Not yet supported */ | |
3861aa5c | 245 | /* |
8bde7f77 WD |
246 | * Binary information to exchange specific |
247 | * product information. | |
3861aa5c WD |
248 | */ |
249 | break; | |
232c150a WD |
250 | /* Reserved (custom) fields (128..254) */ |
251 | } | |
3861aa5c WD |
252 | } |
253 | ||
232c150a | 254 | static void BootpVendorProcess (u8 * ext, int size) |
3861aa5c | 255 | { |
232c150a | 256 | u8 *end = ext + size; |
3861aa5c | 257 | |
232c150a | 258 | debug_ext ("[BOOTP] Checking extension (%d bytes)...\n", size); |
3861aa5c | 259 | |
232c150a WD |
260 | while ((ext < end) && (*ext != 0xff)) { |
261 | if (*ext == 0) { | |
262 | ext++; | |
263 | } else { | |
264 | u8 *opt = ext; | |
265 | ||
266 | ext += ext[1] + 2; | |
267 | if (ext <= end) | |
268 | BootpVendorFieldProcess (opt); | |
269 | } | |
3861aa5c | 270 | } |
3861aa5c WD |
271 | |
272 | #ifdef DEBUG_BOOTP_EXT | |
4b9206ed | 273 | puts ("[BOOTP] Received fields: \n"); |
232c150a WD |
274 | if (NetOurSubnetMask) { |
275 | puts ("NetOurSubnetMask : "); | |
276 | print_IPaddr (NetOurSubnetMask); | |
277 | putc ('\n'); | |
278 | } | |
279 | ||
280 | if (NetOurGatewayIP) { | |
281 | puts ("NetOurGatewayIP : "); | |
282 | print_IPaddr (NetOurGatewayIP); | |
283 | putc ('\n'); | |
284 | } | |
285 | ||
286 | if (NetBootFileSize) { | |
287 | printf ("NetBootFileSize : %d\n", NetBootFileSize); | |
288 | } | |
3861aa5c | 289 | |
232c150a WD |
290 | if (NetOurHostName[0]) { |
291 | printf ("NetOurHostName : %s\n", NetOurHostName); | |
292 | } | |
293 | ||
294 | if (NetOurRootPath[0]) { | |
295 | printf ("NetOurRootPath : %s\n", NetOurRootPath); | |
296 | } | |
297 | ||
298 | if (NetOurNISDomain[0]) { | |
299 | printf ("NetOurNISDomain : %s\n", NetOurNISDomain); | |
300 | } | |
301 | ||
302 | if (NetBootFileSize) { | |
303 | printf ("NetBootFileSize: %d\n", NetBootFileSize); | |
304 | } | |
305 | #endif /* DEBUG_BOOTP_EXT */ | |
306 | } | |
3861aa5c WD |
307 | /* |
308 | * Handle a BOOTP received packet. | |
309 | */ | |
310 | static void | |
311 | BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) | |
312 | { | |
313 | Bootp_t *bp; | |
314 | char *s; | |
315 | ||
316 | debug ("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%d)\n", | |
317 | src, dest, len, sizeof (Bootp_t)); | |
318 | ||
319 | bp = (Bootp_t *)pkt; | |
320 | ||
232c150a | 321 | if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ |
3861aa5c WD |
322 | return; |
323 | ||
324 | /* | |
232c150a | 325 | * Got a good BOOTP reply. Copy the data into our variables. |
3861aa5c WD |
326 | */ |
327 | #ifdef CONFIG_STATUS_LED | |
328 | status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF); | |
329 | #endif | |
330 | ||
331 | BootpCopyNetParams(bp); /* Store net parameters from reply */ | |
332 | ||
333 | /* Retrieve extended information (we must parse the vendor area) */ | |
334 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) | |
77ddac94 | 335 | BootpVendorProcess((uchar *)&bp->bp_vend[4], len); |
3861aa5c WD |
336 | |
337 | NetSetTimeout(0, (thand_f *)0); | |
338 | ||
339 | debug ("Got good BOOTP\n"); | |
340 | ||
132ba5fd WD |
341 | if ((s = getenv("autoload")) != NULL) { |
342 | if (*s == 'n') { | |
343 | /* | |
344 | * Just use BOOTP to configure system; | |
345 | * Do not use TFTP to load the bootfile. | |
346 | */ | |
347 | NetState = NETLOOP_SUCCESS; | |
348 | return; | |
643d1ab2 | 349 | #if defined(CONFIG_CMD_NFS) |
132ba5fd WD |
350 | } else if (strcmp(s, "NFS") == 0) { |
351 | /* | |
352 | * Use NFS to load the bootfile. | |
353 | */ | |
354 | NfsStart(); | |
355 | return; | |
0e6d798c | 356 | #endif |
132ba5fd | 357 | } |
3861aa5c WD |
358 | } |
359 | ||
73a8b27c | 360 | TftpStart(); |
3861aa5c | 361 | } |
610f2e9c | 362 | #endif |
3861aa5c WD |
363 | |
364 | /* | |
365 | * Timeout on BOOTP/DHCP request. | |
366 | */ | |
367 | static void | |
368 | BootpTimeout(void) | |
369 | { | |
370 | if (BootpTry >= TIMEOUT_COUNT) { | |
371 | puts ("\nRetry count exceeded; starting again\n"); | |
372 | NetStartAgain (); | |
373 | } else { | |
374 | NetSetTimeout (TIMEOUT * CFG_HZ, BootpTimeout); | |
375 | BootpRequest (); | |
376 | } | |
377 | } | |
378 | ||
379 | /* | |
380 | * Initialize BOOTP extension fields in the request. | |
381 | */ | |
643d1ab2 | 382 | #if defined(CONFIG_CMD_DHCP) |
232c150a | 383 | static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) |
3861aa5c | 384 | { |
232c150a WD |
385 | u8 *start = e; |
386 | u8 *cnt; | |
387 | ||
1fe80d79 | 388 | #if defined(CONFIG_BOOTP_VENDOREX) |
232c150a | 389 | u8 *x; |
3861aa5c | 390 | #endif |
1fe80d79 | 391 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
77ddac94 | 392 | char *hostname; |
fe389a82 | 393 | #endif |
3861aa5c | 394 | |
232c150a WD |
395 | *e++ = 99; /* RFC1048 Magic Cookie */ |
396 | *e++ = 130; | |
397 | *e++ = 83; | |
398 | *e++ = 99; | |
3861aa5c | 399 | |
232c150a WD |
400 | *e++ = 53; /* DHCP Message Type */ |
401 | *e++ = 1; | |
402 | *e++ = message_type; | |
3861aa5c | 403 | |
232c150a WD |
404 | *e++ = 57; /* Maximum DHCP Message Size */ |
405 | *e++ = 2; | |
406 | *e++ = (576 - 312 + OPT_SIZE) >> 8; | |
407 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; | |
3861aa5c | 408 | |
232c150a WD |
409 | if (ServerID) { |
410 | int tmp = ntohl (ServerID); | |
3861aa5c | 411 | |
232c150a WD |
412 | *e++ = 54; /* ServerID */ |
413 | *e++ = 4; | |
414 | *e++ = tmp >> 24; | |
415 | *e++ = tmp >> 16; | |
416 | *e++ = tmp >> 8; | |
417 | *e++ = tmp & 0xff; | |
418 | } | |
3861aa5c | 419 | |
232c150a WD |
420 | if (RequestedIP) { |
421 | int tmp = ntohl (RequestedIP); | |
3861aa5c | 422 | |
232c150a WD |
423 | *e++ = 50; /* Requested IP */ |
424 | *e++ = 4; | |
425 | *e++ = tmp >> 24; | |
426 | *e++ = tmp >> 16; | |
427 | *e++ = tmp >> 8; | |
428 | *e++ = tmp & 0xff; | |
429 | } | |
1fe80d79 | 430 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
232c150a WD |
431 | if ((hostname = getenv ("hostname"))) { |
432 | int hostnamelen = strlen (hostname); | |
433 | ||
434 | *e++ = 12; /* Hostname */ | |
435 | *e++ = hostnamelen; | |
436 | memcpy (e, hostname, hostnamelen); | |
437 | e += hostnamelen; | |
438 | } | |
fe389a82 SR |
439 | #endif |
440 | ||
1fe80d79 | 441 | #if defined(CONFIG_BOOTP_VENDOREX) |
232c150a WD |
442 | if ((x = dhcp_vendorex_prep (e))) |
443 | return x - start; | |
3861aa5c WD |
444 | #endif |
445 | ||
232c150a WD |
446 | *e++ = 55; /* Parameter Request List */ |
447 | cnt = e++; /* Pointer to count of requested items */ | |
448 | *cnt = 0; | |
1fe80d79 | 449 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
232c150a WD |
450 | *e++ = 1; /* Subnet Mask */ |
451 | *cnt += 1; | |
3861aa5c | 452 | #endif |
1fe80d79 | 453 | #if defined(CONFIG_BOOTP_TIMEOFFSET) |
ea287deb WD |
454 | *e++ = 2; |
455 | *cnt += 1; | |
456 | #endif | |
1fe80d79 | 457 | #if defined(CONFIG_BOOTP_GATEWAY) |
232c150a WD |
458 | *e++ = 3; /* Router Option */ |
459 | *cnt += 1; | |
3861aa5c | 460 | #endif |
1fe80d79 | 461 | #if defined(CONFIG_BOOTP_DNS) |
232c150a WD |
462 | *e++ = 6; /* DNS Server(s) */ |
463 | *cnt += 1; | |
3861aa5c | 464 | #endif |
1fe80d79 | 465 | #if defined(CONFIG_BOOTP_HOSTNAME) |
232c150a WD |
466 | *e++ = 12; /* Hostname */ |
467 | *cnt += 1; | |
3861aa5c | 468 | #endif |
1fe80d79 | 469 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
232c150a WD |
470 | *e++ = 13; /* Boot File Size */ |
471 | *cnt += 1; | |
3861aa5c | 472 | #endif |
1fe80d79 | 473 | #if defined(CONFIG_BOOTP_BOOTPATH) |
232c150a WD |
474 | *e++ = 17; /* Boot path */ |
475 | *cnt += 1; | |
3861aa5c | 476 | #endif |
1fe80d79 | 477 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
232c150a WD |
478 | *e++ = 40; /* NIS Domain name request */ |
479 | *cnt += 1; | |
ea287deb | 480 | #endif |
1fe80d79 | 481 | #if defined(CONFIG_BOOTP_NTPSERVER) |
ea287deb WD |
482 | *e++ = 42; |
483 | *cnt += 1; | |
3861aa5c | 484 | #endif |
232c150a | 485 | *e++ = 255; /* End of the list */ |
3861aa5c | 486 | |
232c150a | 487 | /* Pad to minimal length */ |
3861aa5c | 488 | #ifdef CONFIG_DHCP_MIN_EXT_LEN |
232c150a WD |
489 | while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN) |
490 | *e++ = 0; | |
3861aa5c WD |
491 | #endif |
492 | ||
232c150a | 493 | return e - start; |
3861aa5c WD |
494 | } |
495 | ||
610f2e9c | 496 | #else |
3861aa5c | 497 | /* |
1fe80d79 | 498 | * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! |
3861aa5c | 499 | */ |
232c150a | 500 | static int BootpExtended (u8 * e) |
3861aa5c | 501 | { |
232c150a | 502 | u8 *start = e; |
3861aa5c | 503 | |
232c150a WD |
504 | *e++ = 99; /* RFC1048 Magic Cookie */ |
505 | *e++ = 130; | |
506 | *e++ = 83; | |
507 | *e++ = 99; | |
3861aa5c | 508 | |
643d1ab2 | 509 | #if defined(CONFIG_CMD_DHCP) |
232c150a WD |
510 | *e++ = 53; /* DHCP Message Type */ |
511 | *e++ = 1; | |
512 | *e++ = DHCP_DISCOVER; | |
513 | ||
514 | *e++ = 57; /* Maximum DHCP Message Size */ | |
515 | *e++ = 2; | |
516 | *e++ = (576 - 312 + OPT_SIZE) >> 16; | |
517 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; | |
610f2e9c | 518 | #endif |
3861aa5c | 519 | |
1fe80d79 | 520 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
232c150a WD |
521 | *e++ = 1; /* Subnet mask request */ |
522 | *e++ = 4; | |
523 | e += 4; | |
3861aa5c WD |
524 | #endif |
525 | ||
1fe80d79 | 526 | #if defined(CONFIG_BOOTP_GATEWAY) |
232c150a WD |
527 | *e++ = 3; /* Default gateway request */ |
528 | *e++ = 4; | |
529 | e += 4; | |
3861aa5c WD |
530 | #endif |
531 | ||
1fe80d79 | 532 | #if defined(CONFIG_BOOTP_DNS) |
232c150a WD |
533 | *e++ = 6; /* Domain Name Server */ |
534 | *e++ = 4; | |
535 | e += 4; | |
3861aa5c WD |
536 | #endif |
537 | ||
1fe80d79 | 538 | #if defined(CONFIG_BOOTP_HOSTNAME) |
232c150a WD |
539 | *e++ = 12; /* Host name request */ |
540 | *e++ = 32; | |
541 | e += 32; | |
3861aa5c WD |
542 | #endif |
543 | ||
1fe80d79 | 544 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
232c150a WD |
545 | *e++ = 13; /* Boot file size */ |
546 | *e++ = 2; | |
547 | e += 2; | |
3861aa5c WD |
548 | #endif |
549 | ||
1fe80d79 | 550 | #if defined(CONFIG_BOOTP_BOOTPATH) |
232c150a WD |
551 | *e++ = 17; /* Boot path */ |
552 | *e++ = 32; | |
553 | e += 32; | |
3861aa5c WD |
554 | #endif |
555 | ||
1fe80d79 | 556 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
232c150a WD |
557 | *e++ = 40; /* NIS Domain name request */ |
558 | *e++ = 32; | |
559 | e += 32; | |
3861aa5c WD |
560 | #endif |
561 | ||
232c150a | 562 | *e++ = 255; /* End of the list */ |
3861aa5c | 563 | |
232c150a | 564 | return e - start; |
3861aa5c | 565 | } |
610f2e9c | 566 | #endif |
3861aa5c WD |
567 | |
568 | void | |
569 | BootpRequest (void) | |
570 | { | |
571 | volatile uchar *pkt, *iphdr; | |
572 | Bootp_t *bp; | |
573 | int ext_len, pktlen, iplen; | |
574 | ||
643d1ab2 | 575 | #if defined(CONFIG_CMD_DHCP) |
3861aa5c WD |
576 | dhcp_state = INIT; |
577 | #endif | |
578 | ||
579 | #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ | |
580 | unsigned char bi_enetaddr[6]; | |
581 | int reg; | |
582 | char *e,*s; | |
77ddac94 | 583 | char tmp[64]; |
3861aa5c WD |
584 | ulong tst1, tst2, sum, m_mask, m_value = 0; |
585 | ||
586 | if (BootpTry ==0) { | |
587 | /* get our mac */ | |
588 | reg = getenv_r ("ethaddr", tmp, sizeof(tmp)); | |
589 | s = (reg > 0) ? tmp : NULL; | |
590 | ||
591 | for (reg=0; reg<6; ++reg) { | |
592 | bi_enetaddr[reg] = s ? simple_strtoul(s, &e, 16) : 0; | |
593 | if (s) { | |
594 | s = (*e) ? e+1 : e; | |
595 | } | |
596 | } | |
597 | #ifdef DEBUG | |
4b9206ed | 598 | puts ("BootpRequest => Our Mac: "); |
3861aa5c WD |
599 | for (reg=0; reg<6; reg++) { |
600 | printf ("%x%c", | |
601 | bi_enetaddr[reg], | |
602 | reg==5 ? '\n' : ':'); | |
603 | } | |
604 | #endif /* DEBUG */ | |
605 | ||
606 | /* Mac-Manipulation 2 get seed1 */ | |
607 | tst1=0; | |
608 | tst2=0; | |
609 | for (reg=2; reg<6; reg++) { | |
610 | tst1 = tst1 << 8; | |
611 | tst1 = tst1 | bi_enetaddr[reg]; | |
612 | } | |
613 | for (reg=0; reg<2; reg++) { | |
614 | tst2 = tst2 | bi_enetaddr[reg]; | |
615 | tst2 = tst2 << 8; | |
616 | } | |
617 | ||
618 | seed1 = tst1^tst2; | |
619 | ||
620 | /* Mirror seed1*/ | |
621 | m_mask=0x1; | |
622 | for (reg=1;reg<=32;reg++) { | |
623 | m_value |= (m_mask & seed1); | |
624 | seed1 = seed1 >> 1; | |
625 | m_value = m_value << 1; | |
626 | } | |
627 | seed1 = m_value; | |
628 | seed2 = 0xB78D0945; | |
629 | } | |
630 | ||
631 | /* Random Number Generator */ | |
632 | ||
633 | for (reg=0;reg<=0;reg++) { | |
634 | sum = seed1 + seed2; | |
635 | if (sum < seed1 || sum < seed2) | |
636 | sum++; | |
8bde7f77 | 637 | seed2 = seed1; |
3861aa5c WD |
638 | seed1 = sum; |
639 | ||
640 | if (BootpTry<=2) { /* Start with max 1024 * 1ms */ | |
641 | sum = sum >> (22-BootpTry); | |
642 | } else { /*After 3rd BOOTP request max 8192 * 1ms */ | |
643 | sum = sum >> 19; | |
644 | } | |
645 | } | |
646 | ||
647 | printf ("Random delay: %ld ms...\n", sum); | |
648 | for (reg=0; reg <sum; reg++) { | |
649 | udelay(1000); /*Wait 1ms*/ | |
650 | } | |
651 | #endif /* CONFIG_BOOTP_RANDOM_DELAY */ | |
652 | ||
653 | printf("BOOTP broadcast %d\n", ++BootpTry); | |
654 | pkt = NetTxPacket; | |
655 | memset ((void*)pkt, 0, PKTSIZE); | |
656 | ||
a3d991bd | 657 | pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); |
3861aa5c WD |
658 | |
659 | /* | |
660 | * Next line results in incorrect packet size being transmitted, resulting | |
661 | * in errors in some DHCP servers, reporting missing bytes. Size must be | |
662 | * set in packet header after extension length has been determined. | |
663 | * C. Hallinan, DS4.COM, Inc. | |
664 | */ | |
665 | /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ | |
666 | iphdr = pkt; /* We need this later for NetSetIP() */ | |
667 | pkt += IP_HDR_SIZE; | |
668 | ||
669 | bp = (Bootp_t *)pkt; | |
670 | bp->bp_op = OP_BOOTREQUEST; | |
671 | bp->bp_htype = HWT_ETHER; | |
672 | bp->bp_hlen = HWL_ETHER; | |
673 | bp->bp_hops = 0; | |
674 | bp->bp_secs = htons(get_timer(0) / CFG_HZ); | |
675 | NetWriteIP(&bp->bp_ciaddr, 0); | |
676 | NetWriteIP(&bp->bp_yiaddr, 0); | |
677 | NetWriteIP(&bp->bp_siaddr, 0); | |
678 | NetWriteIP(&bp->bp_giaddr, 0); | |
679 | memcpy (bp->bp_chaddr, NetOurEther, 6); | |
680 | copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file)); | |
681 | ||
682 | /* Request additional information from the BOOTP/DHCP server */ | |
643d1ab2 | 683 | #if defined(CONFIG_CMD_DHCP) |
77ddac94 | 684 | ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); |
3861aa5c | 685 | #else |
77ddac94 | 686 | ext_len = BootpExtended((u8 *)bp->bp_vend); |
610f2e9c | 687 | #endif |
3861aa5c WD |
688 | |
689 | /* | |
690 | * Bootp ID is the lower 4 bytes of our ethernet address | |
691 | * plus the current time in HZ. | |
692 | */ | |
693 | BootpID = ((ulong)NetOurEther[2] << 24) | |
694 | | ((ulong)NetOurEther[3] << 16) | |
695 | | ((ulong)NetOurEther[4] << 8) | |
696 | | (ulong)NetOurEther[5]; | |
697 | BootpID += get_timer(0); | |
232c150a | 698 | BootpID = htonl(BootpID); |
3861aa5c WD |
699 | NetCopyLong(&bp->bp_id, &BootpID); |
700 | ||
701 | /* | |
702 | * Calculate proper packet lengths taking into account the | |
703 | * variable size of the options field | |
704 | */ | |
705 | pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + ext_len; | |
706 | iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; | |
707 | NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); | |
708 | NetSetTimeout(SELECT_TIMEOUT * CFG_HZ, BootpTimeout); | |
709 | ||
643d1ab2 | 710 | #if defined(CONFIG_CMD_DHCP) |
3861aa5c WD |
711 | dhcp_state = SELECTING; |
712 | NetSetHandler(DhcpHandler); | |
713 | #else | |
714 | NetSetHandler(BootpHandler); | |
610f2e9c | 715 | #endif |
3861aa5c WD |
716 | NetSendPacket(NetTxPacket, pktlen); |
717 | } | |
718 | ||
643d1ab2 | 719 | #if defined(CONFIG_CMD_DHCP) |
3b2e4fd9 | 720 | static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) |
3861aa5c | 721 | { |
3e38691e | 722 | uchar *end = popt + BOOTP_HDR_SIZE; |
3861aa5c WD |
723 | int oplen, size; |
724 | ||
232c150a | 725 | while (popt < end && *popt != 0xff) { |
3861aa5c | 726 | oplen = *(popt + 1); |
232c150a WD |
727 | switch (*popt) { |
728 | case 1: | |
729 | NetCopyIP (&NetOurSubnetMask, (popt + 2)); | |
730 | break; | |
1fe80d79 | 731 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
ea287deb | 732 | case 2: /* Time offset */ |
135e19bc | 733 | NetCopyLong ((ulong *)&NetTimeOffset, (ulong *) (popt + 2)); |
ea287deb WD |
734 | NetTimeOffset = ntohl (NetTimeOffset); |
735 | break; | |
736 | #endif | |
232c150a WD |
737 | case 3: |
738 | NetCopyIP (&NetOurGatewayIP, (popt + 2)); | |
739 | break; | |
740 | case 6: | |
741 | NetCopyIP (&NetOurDNSIP, (popt + 2)); | |
1fe80d79 | 742 | #if defined(CONFIG_BOOTP_DNS2) |
232c150a WD |
743 | if (*(popt + 1) > 4) { |
744 | NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); | |
745 | } | |
fe389a82 | 746 | #endif |
232c150a WD |
747 | break; |
748 | case 12: | |
749 | size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); | |
750 | memcpy (&NetOurHostName, popt + 2, size); | |
751 | NetOurHostName[size] = 0; | |
752 | break; | |
753 | case 15: /* Ignore Domain Name Option */ | |
754 | break; | |
755 | case 17: | |
756 | size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); | |
757 | memcpy (&NetOurRootPath, popt + 2, size); | |
758 | NetOurRootPath[size] = 0; | |
759 | break; | |
1fe80d79 | 760 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
ea287deb WD |
761 | case 42: /* NTP server IP */ |
762 | NetCopyIP (&NetNtpServerIP, (popt + 2)); | |
763 | break; | |
764 | #endif | |
232c150a WD |
765 | case 51: |
766 | NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); | |
767 | break; | |
768 | case 53: /* Ignore Message Type Option */ | |
769 | break; | |
770 | case 54: | |
771 | NetCopyIP (&NetDHCPServerIP, (popt + 2)); | |
772 | break; | |
773 | case 58: /* Ignore Renewal Time Option */ | |
774 | break; | |
775 | case 59: /* Ignore Rebinding Time Option */ | |
776 | break; | |
3b2e4fd9 WD |
777 | case 66: /* Ignore TFTP server name */ |
778 | break; | |
779 | case 67: /* vendor opt bootfile */ | |
780 | /* | |
781 | * I can't use dhcp_vendorex_proc here because I need | |
782 | * to write into the bootp packet - even then I had to | |
783 | * pass the bootp packet pointer into here as the | |
784 | * second arg | |
785 | */ | |
786 | size = truncate_sz ("Opt Boot File", | |
787 | sizeof(bp->bp_file), | |
788 | oplen); | |
789 | if (bp->bp_file[0] == '\0' && size > 0) { | |
790 | /* | |
791 | * only use vendor boot file if we didn't | |
792 | * receive a boot file in the main non-vendor | |
793 | * part of the packet - god only knows why | |
794 | * some vendors chose not to use this perfectly | |
795 | * good spot to store the boot file (join on | |
796 | * Tru64 Unix) it seems mind bogglingly crazy | |
797 | * to me | |
798 | */ | |
799 | printf("*** WARNING: using vendor " | |
800 | "optional boot file\n"); | |
801 | memcpy(bp->bp_file, popt + 2, size); | |
802 | bp->bp_file[size] = '\0'; | |
803 | } | |
804 | break; | |
232c150a | 805 | default: |
1fe80d79 | 806 | #if defined(CONFIG_BOOTP_VENDOREX) |
232c150a | 807 | if (dhcp_vendorex_proc (popt)) |
8bde7f77 | 808 | break; |
3861aa5c | 809 | #endif |
232c150a WD |
810 | printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); |
811 | break; | |
3861aa5c WD |
812 | } |
813 | popt += oplen + 2; /* Process next option */ | |
814 | } | |
815 | } | |
816 | ||
817 | static int DhcpMessageType(unsigned char *popt) | |
818 | { | |
819 | if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) | |
820 | return -1; | |
821 | ||
822 | popt += 4; | |
823 | while ( *popt != 0xff ) { | |
824 | if ( *popt == 53 ) /* DHCP Message Type */ | |
825 | return *(popt + 2); | |
826 | popt += *(popt + 1) + 2; /* Scan through all options */ | |
827 | } | |
828 | return -1; | |
829 | } | |
830 | ||
3e38691e | 831 | static void DhcpSendRequestPkt(Bootp_t *bp_offer) |
3861aa5c WD |
832 | { |
833 | volatile uchar *pkt, *iphdr; | |
834 | Bootp_t *bp; | |
835 | int pktlen, iplen, extlen; | |
47cd00fa | 836 | IPaddr_t OfferedIP; |
3861aa5c WD |
837 | |
838 | debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); | |
839 | pkt = NetTxPacket; | |
840 | memset ((void*)pkt, 0, PKTSIZE); | |
841 | ||
a3d991bd | 842 | pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); |
3861aa5c WD |
843 | |
844 | iphdr = pkt; /* We'll need this later to set proper pkt size */ | |
845 | pkt += IP_HDR_SIZE; | |
846 | ||
847 | bp = (Bootp_t *)pkt; | |
848 | bp->bp_op = OP_BOOTREQUEST; | |
849 | bp->bp_htype = HWT_ETHER; | |
850 | bp->bp_hlen = HWL_ETHER; | |
851 | bp->bp_hops = 0; | |
852 | bp->bp_secs = htons(get_timer(0) / CFG_HZ); | |
853 | NetCopyIP(&bp->bp_ciaddr, &bp_offer->bp_ciaddr); /* both in network byte order */ | |
854 | NetCopyIP(&bp->bp_yiaddr, &bp_offer->bp_yiaddr); | |
855 | NetCopyIP(&bp->bp_siaddr, &bp_offer->bp_siaddr); | |
c6686703 | 856 | /* |
d82718fe WD |
857 | * RFC3046 requires Relay Agents to discard packets with |
858 | * nonzero and offered giaddr | |
859 | */ | |
860 | NetWriteIP(&bp->bp_giaddr, 0); | |
861 | ||
3861aa5c WD |
862 | memcpy (bp->bp_chaddr, NetOurEther, 6); |
863 | ||
864 | /* | |
865 | * ID is the id of the OFFER packet | |
866 | */ | |
867 | ||
868 | NetCopyLong(&bp->bp_id, &bp_offer->bp_id); | |
869 | ||
870 | /* | |
871 | * Copy options from OFFER packet if present | |
872 | */ | |
47cd00fa | 873 | NetCopyIP(&OfferedIP, &bp->bp_yiaddr); |
77ddac94 | 874 | extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); |
3861aa5c WD |
875 | |
876 | pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen; | |
877 | iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; | |
878 | NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); | |
879 | ||
880 | debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); | |
881 | NetSendPacket(NetTxPacket, pktlen); | |
882 | } | |
883 | ||
884 | /* | |
885 | * Handle DHCP received packets. | |
886 | */ | |
887 | static void | |
888 | DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) | |
889 | { | |
890 | Bootp_t *bp = (Bootp_t *)pkt; | |
891 | ||
892 | debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", | |
893 | src, dest, len, dhcp_state); | |
894 | ||
232c150a | 895 | if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ |
3861aa5c WD |
896 | return; |
897 | ||
898 | debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n", | |
899 | src, dest, len, dhcp_state); | |
900 | ||
901 | switch (dhcp_state) { | |
902 | case SELECTING: | |
903 | /* | |
904 | * Wait an appropriate time for any potential DHCPOFFER packets | |
905 | * to arrive. Then select one, and generate DHCPREQUEST response. | |
906 | * If filename is in format we recognize, assume it is a valid | |
907 | * OFFER from a server we want. | |
908 | */ | |
909 | debug ("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file); | |
910 | #ifdef CFG_BOOTFILE_PREFIX | |
911 | if (strncmp(bp->bp_file, | |
912 | CFG_BOOTFILE_PREFIX, | |
913 | strlen(CFG_BOOTFILE_PREFIX)) == 0 ) { | |
914 | #endif /* CFG_BOOTFILE_PREFIX */ | |
915 | ||
916 | debug ("TRANSITIONING TO REQUESTING STATE\n"); | |
917 | dhcp_state = REQUESTING; | |
759a51b4 | 918 | |
3861aa5c | 919 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
3b2e4fd9 | 920 | DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); |
3861aa5c | 921 | |
232c150a | 922 | BootpCopyNetParams(bp); /* Store net params from reply */ |
3861aa5c WD |
923 | |
924 | NetSetTimeout(TIMEOUT * CFG_HZ, BootpTimeout); | |
925 | DhcpSendRequestPkt(bp); | |
926 | #ifdef CFG_BOOTFILE_PREFIX | |
927 | } | |
928 | #endif /* CFG_BOOTFILE_PREFIX */ | |
929 | ||
930 | return; | |
931 | break; | |
932 | case REQUESTING: | |
933 | debug ("DHCP State: REQUESTING\n"); | |
934 | ||
77ddac94 | 935 | if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) { |
3861aa5c WD |
936 | char *s; |
937 | ||
938 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) | |
3b2e4fd9 | 939 | DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); |
232c150a | 940 | BootpCopyNetParams(bp); /* Store net params from reply */ |
3861aa5c | 941 | dhcp_state = BOUND; |
4b9206ed | 942 | puts ("DHCP client bound to address "); |
3861aa5c | 943 | print_IPaddr(NetOurIP); |
4b9206ed | 944 | putc ('\n'); |
3861aa5c WD |
945 | |
946 | /* Obey the 'autoload' setting */ | |
132ba5fd WD |
947 | if ((s = getenv("autoload")) != NULL) { |
948 | if (*s == 'n') { | |
949 | /* | |
950 | * Just use BOOTP to configure system; | |
951 | * Do not use TFTP to load the bootfile. | |
952 | */ | |
953 | NetState = NETLOOP_SUCCESS; | |
954 | return; | |
643d1ab2 | 955 | #if defined(CONFIG_CMD_NFS) |
132ba5fd WD |
956 | } else if (strcmp(s, "NFS") == 0) { |
957 | /* | |
958 | * Use NFS to load the bootfile. | |
959 | */ | |
960 | NfsStart(); | |
961 | return; | |
0e6d798c | 962 | #endif |
132ba5fd | 963 | } |
3861aa5c | 964 | } |
73a8b27c | 965 | TftpStart(); |
3861aa5c WD |
966 | return; |
967 | } | |
968 | break; | |
969 | default: | |
4b9206ed | 970 | puts ("DHCP: INVALID STATE\n"); |
3861aa5c WD |
971 | break; |
972 | } | |
973 | ||
974 | } | |
975 | ||
976 | void DhcpRequest(void) | |
977 | { | |
978 | BootpRequest(); | |
979 | } | |
610f2e9c | 980 | #endif |
3861aa5c | 981 | |
610f2e9c | 982 | #endif |