bootp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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
  8. * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <net.h>
  13. #include "bootp.h"
  14. #include "tftp.h"
  15. #include "nfs.h"
  16. #ifdef CONFIG_STATUS_LED
  17. #include <status_led.h>
  18. #endif
  19. #ifdef CONFIG_BOOTP_RANDOM_DELAY
  20. #include "net_rand.h"
  21. #endif
  22. #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
  23. /*
  24. * The timeout for the initial BOOTP/DHCP request used to be described by a
  25. * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
  26. * that counter
  27. *
  28. * Now that the timeout periods are variable (exponential backoff and retry)
  29. * we convert the timeout count to the absolute time it would have take to
  30. * execute that many retries, and keep sending retry packets until that time
  31. * is reached.
  32. */
  33. #ifndef CONFIG_NET_RETRY_COUNT
  34. # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  35. #else
  36. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  37. #endif
  38. #define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
  39. #define PORT_BOOTPS 67 /* BOOTP server UDP port */
  40. #define PORT_BOOTPC 68 /* BOOTP client UDP port */
  41. #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
  42. #define CONFIG_DHCP_MIN_EXT_LEN 64
  43. #endif
  44. #ifndef CONFIG_BOOTP_ID_CACHE_SIZE
  45. #define CONFIG_BOOTP_ID_CACHE_SIZE 4
  46. #endif
  47. ulong bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
  48. unsigned int bootp_num_ids;
  49. int bootp_try;
  50. ulong bootp_start;
  51. ulong bootp_timeout;
  52. char net_nis_domain[32] = {0,}; /* Our NIS domain */
  53. char net_hostname[32] = {0,}; /* Our hostname */
  54. char net_root_path[64] = {0,}; /* Our bootpath */
  55. #if defined(CONFIG_CMD_DHCP)
  56. static dhcp_state_t dhcp_state = INIT;
  57. static unsigned long dhcp_leasetime;
  58. static struct in_addr dhcp_server_ip;
  59. static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  60. unsigned src, unsigned len);
  61. /* For Debug */
  62. #if 0
  63. static char *dhcpmsg2str(int type)
  64. {
  65. switch (type) {
  66. case 1: return "DHCPDISCOVER"; break;
  67. case 2: return "DHCPOFFER"; break;
  68. case 3: return "DHCPREQUEST"; break;
  69. case 4: return "DHCPDECLINE"; break;
  70. case 5: return "DHCPACK"; break;
  71. case 6: return "DHCPNACK"; break;
  72. case 7: return "DHCPRELEASE"; break;
  73. default: return "UNKNOWN/INVALID MSG TYPE"; break;
  74. }
  75. }
  76. #endif
  77. #endif
  78. static void bootp_add_id(ulong id)
  79. {
  80. if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
  81. size_t size = sizeof(bootp_ids) - sizeof(id);
  82. memmove(bootp_ids, &bootp_ids[1], size);
  83. bootp_ids[bootp_num_ids - 1] = id;
  84. } else {
  85. bootp_ids[bootp_num_ids] = id;
  86. bootp_num_ids++;
  87. }
  88. }
  89. static bool bootp_match_id(ulong id)
  90. {
  91. unsigned int i;
  92. for (i = 0; i < bootp_num_ids; i++)
  93. if (bootp_ids[i] == id)
  94. return true;
  95. return false;
  96. }
  97. static int check_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  98. {
  99. struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
  100. int retval = 0;
  101. if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
  102. retval = -1;
  103. else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
  104. retval = -2;
  105. else if (bp->bp_op != OP_BOOTREQUEST &&
  106. bp->bp_op != OP_BOOTREPLY &&
  107. bp->bp_op != DHCP_OFFER &&
  108. bp->bp_op != DHCP_ACK &&
  109. bp->bp_op != DHCP_NAK)
  110. retval = -3;
  111. else if (bp->bp_htype != HWT_ETHER)
  112. retval = -4;
  113. else if (bp->bp_hlen != HWL_ETHER)
  114. retval = -5;
  115. else if (!bootp_match_id(net_read_long((ulong *)&bp->bp_id)))
  116. retval = -6;
  117. debug("Filtering pkt = %d\n", retval);
  118. return retval;
  119. }
  120. /*
  121. * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
  122. */
  123. static void store_net_params(struct bootp_hdr *bp)
  124. {
  125. #if !defined(CONFIG_BOOTP_SERVERIP)
  126. struct in_addr tmp_ip;
  127. net_copy_ip(&tmp_ip, &bp->bp_siaddr);
  128. if (tmp_ip.s_addr != 0)
  129. net_copy_ip(&net_server_ip, &bp->bp_siaddr);
  130. memcpy(net_server_ethaddr,
  131. ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
  132. if (strlen(bp->bp_file) > 0)
  133. copy_filename(net_boot_file_name, bp->bp_file,
  134. sizeof(net_boot_file_name));
  135. debug("net_boot_file_name: %s\n", net_boot_file_name);
  136. /* Propagate to environment:
  137. * don't delete exising entry when BOOTP / DHCP reply does
  138. * not contain a new value
  139. */
  140. if (*net_boot_file_name)
  141. setenv("bootfile", net_boot_file_name);
  142. #endif
  143. net_copy_ip(&net_ip, &bp->bp_yiaddr);
  144. }
  145. static int truncate_sz(const char *name, int maxlen, int curlen)
  146. {
  147. if (curlen >= maxlen) {
  148. printf("*** WARNING: %s is too long (%d - max: %d)"
  149. " - truncated\n", name, curlen, maxlen);
  150. curlen = maxlen - 1;
  151. }
  152. return curlen;
  153. }
  154. #if !defined(CONFIG_CMD_DHCP)
  155. static void bootp_process_vendor_field(u8 *ext)
  156. {
  157. int size = *(ext + 1);
  158. debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
  159. *(ext + 1));
  160. net_boot_file_expected_size_in_blocks = 0;
  161. switch (*ext) {
  162. /* Fixed length fields */
  163. case 1: /* Subnet mask */
  164. if (net_netmask.s_addr == 0)
  165. net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
  166. break;
  167. case 2: /* Time offset - Not yet supported */
  168. break;
  169. /* Variable length fields */
  170. case 3: /* Gateways list */
  171. if (net_gateway.s_addr == 0)
  172. net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
  173. break;
  174. case 4: /* Time server - Not yet supported */
  175. break;
  176. case 5: /* IEN-116 name server - Not yet supported */
  177. break;
  178. case 6:
  179. if (net_dns_server.s_addr == 0)
  180. net_copy_ip(&net_dns_server,
  181. (struct in_addr *)(ext + 2));
  182. #if defined(CONFIG_BOOTP_DNS2)
  183. if ((net_dns_server2.s_addr == 0) && (size > 4))
  184. net_copy_ip(&net_dns_server2,
  185. (struct in_addr *)(ext + 2 + 4));
  186. #endif
  187. break;
  188. case 7: /* Log server - Not yet supported */
  189. break;
  190. case 8: /* Cookie/Quote server - Not yet supported */
  191. break;
  192. case 9: /* LPR server - Not yet supported */
  193. break;
  194. case 10: /* Impress server - Not yet supported */
  195. break;
  196. case 11: /* RPL server - Not yet supported */
  197. break;
  198. case 12: /* Host name */
  199. if (net_hostname[0] == 0) {
  200. size = truncate_sz("Host Name",
  201. sizeof(net_hostname), size);
  202. memcpy(&net_hostname, ext + 2, size);
  203. net_hostname[size] = 0;
  204. }
  205. break;
  206. case 13: /* Boot file size */
  207. if (size == 2)
  208. net_boot_file_expected_size_in_blocks =
  209. ntohs(*(ushort *)(ext + 2));
  210. else if (size == 4)
  211. net_boot_file_expected_size_in_blocks =
  212. ntohl(*(ulong *)(ext + 2));
  213. break;
  214. case 14: /* Merit dump file - Not yet supported */
  215. break;
  216. case 15: /* Domain name - Not yet supported */
  217. break;
  218. case 16: /* Swap server - Not yet supported */
  219. break;
  220. case 17: /* Root path */
  221. if (net_root_path[0] == 0) {
  222. size = truncate_sz("Root Path",
  223. sizeof(net_root_path), size);
  224. memcpy(&net_root_path, ext + 2, size);
  225. net_root_path[size] = 0;
  226. }
  227. break;
  228. case 18: /* Extension path - Not yet supported */
  229. /*
  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.
  233. */
  234. break;
  235. /* IP host layer fields */
  236. case 40: /* NIS Domain name */
  237. if (net_nis_domain[0] == 0) {
  238. size = truncate_sz("NIS Domain Name",
  239. sizeof(net_nis_domain), size);
  240. memcpy(&net_nis_domain, ext + 2, size);
  241. net_nis_domain[size] = 0;
  242. }
  243. break;
  244. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  245. case 42: /* NTP server IP */
  246. net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
  247. break;
  248. #endif
  249. /* Application layer fields */
  250. case 43: /* Vendor specific info - Not yet supported */
  251. /*
  252. * Binary information to exchange specific
  253. * product information.
  254. */
  255. break;
  256. /* Reserved (custom) fields (128..254) */
  257. }
  258. }
  259. static void bootp_process_vendor(u8 *ext, int size)
  260. {
  261. u8 *end = ext + size;
  262. debug("[BOOTP] Checking extension (%d bytes)...\n", size);
  263. while ((ext < end) && (*ext != 0xff)) {
  264. if (*ext == 0) {
  265. ext++;
  266. } else {
  267. u8 *opt = ext;
  268. ext += ext[1] + 2;
  269. if (ext <= end)
  270. bootp_process_vendor_field(opt);
  271. }
  272. }
  273. debug("[BOOTP] Received fields:\n");
  274. if (net_netmask.s_addr)
  275. debug("net_netmask : %pI4\n", &net_netmask);
  276. if (net_gateway.s_addr)
  277. debug("net_gateway : %pI4", &net_gateway);
  278. if (net_boot_file_expected_size_in_blocks)
  279. debug("net_boot_file_expected_size_in_blocks : %d\n",
  280. net_boot_file_expected_size_in_blocks);
  281. if (net_hostname[0])
  282. debug("net_hostname : %s\n", net_hostname);
  283. if (net_root_path[0])
  284. debug("net_root_path : %s\n", net_root_path);
  285. if (net_nis_domain[0])
  286. debug("net_nis_domain : %s\n", net_nis_domain);
  287. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  288. if (net_ntp_server)
  289. debug("net_ntp_server : %pI4\n", &net_ntp_server);
  290. #endif
  291. }
  292. /*
  293. * Handle a BOOTP received packet.
  294. */
  295. static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  296. unsigned src, unsigned len)
  297. {
  298. struct bootp_hdr *bp;
  299. debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
  300. src, dest, len, sizeof(struct bootp_hdr));
  301. bp = (struct bootp_hdr *)pkt;
  302. /* Filter out pkts we don't want */
  303. if (check_packet(pkt, dest, src, len))
  304. return;
  305. /*
  306. * Got a good BOOTP reply. Copy the data into our variables.
  307. */
  308. #ifdef CONFIG_STATUS_LED
  309. status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
  310. #endif
  311. store_net_params(bp); /* Store net parameters from reply */
  312. /* Retrieve extended information (we must parse the vendor area) */
  313. if (net_read_long((ulong *)&bp->bp_vend[0]) ==
  314. htonl(BOOTP_VENDOR_MAGIC))
  315. bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
  316. net_set_timeout_handler(0, (thand_f *)0);
  317. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
  318. debug("Got good BOOTP\n");
  319. net_auto_load();
  320. }
  321. #endif
  322. /*
  323. * Timeout on BOOTP/DHCP request.
  324. */
  325. static void bootp_timeout_handler(void)
  326. {
  327. ulong time_taken = get_timer(bootp_start);
  328. if (time_taken >= TIMEOUT_MS) {
  329. #ifdef CONFIG_BOOTP_MAY_FAIL
  330. puts("\nRetry time exceeded\n");
  331. net_set_state(NETLOOP_FAIL);
  332. #else
  333. puts("\nRetry time exceeded; starting again\n");
  334. net_start_again();
  335. #endif
  336. } else {
  337. bootp_timeout *= 2;
  338. if (bootp_timeout > 2000)
  339. bootp_timeout = 2000;
  340. net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
  341. bootp_request();
  342. }
  343. }
  344. #define put_vci(e, str) \
  345. do { \
  346. size_t vci_strlen = strlen(str); \
  347. *e++ = 60; /* Vendor Class Identifier */ \
  348. *e++ = vci_strlen; \
  349. memcpy(e, str, vci_strlen); \
  350. e += vci_strlen; \
  351. } while (0)
  352. /*
  353. * Initialize BOOTP extension fields in the request.
  354. */
  355. #if defined(CONFIG_CMD_DHCP)
  356. static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
  357. struct in_addr requested_ip)
  358. {
  359. u8 *start = e;
  360. u8 *cnt;
  361. #if defined(CONFIG_BOOTP_PXE)
  362. char *uuid;
  363. u16 clientarch;
  364. #endif
  365. #if defined(CONFIG_BOOTP_VENDOREX)
  366. u8 *x;
  367. #endif
  368. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  369. char *hostname;
  370. #endif
  371. *e++ = 99; /* RFC1048 Magic Cookie */
  372. *e++ = 130;
  373. *e++ = 83;
  374. *e++ = 99;
  375. *e++ = 53; /* DHCP Message Type */
  376. *e++ = 1;
  377. *e++ = message_type;
  378. *e++ = 57; /* Maximum DHCP Message Size */
  379. *e++ = 2;
  380. *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
  381. *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
  382. if (server_ip.s_addr) {
  383. int tmp = ntohl(server_ip.s_addr);
  384. *e++ = 54; /* ServerID */
  385. *e++ = 4;
  386. *e++ = tmp >> 24;
  387. *e++ = tmp >> 16;
  388. *e++ = tmp >> 8;
  389. *e++ = tmp & 0xff;
  390. }
  391. if (requested_ip.s_addr) {
  392. int tmp = ntohl(requested_ip.s_addr);
  393. *e++ = 50; /* Requested IP */
  394. *e++ = 4;
  395. *e++ = tmp >> 24;
  396. *e++ = tmp >> 16;
  397. *e++ = tmp >> 8;
  398. *e++ = tmp & 0xff;
  399. }
  400. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  401. hostname = getenv("hostname");
  402. if (hostname) {
  403. int hostnamelen = strlen(hostname);
  404. *e++ = 12; /* Hostname */
  405. *e++ = hostnamelen;
  406. memcpy(e, hostname, hostnamelen);
  407. e += hostnamelen;
  408. }
  409. #endif
  410. #if defined(CONFIG_BOOTP_PXE)
  411. clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
  412. *e++ = 93; /* Client System Architecture */
  413. *e++ = 2;
  414. *e++ = (clientarch >> 8) & 0xff;
  415. *e++ = clientarch & 0xff;
  416. *e++ = 94; /* Client Network Interface Identifier */
  417. *e++ = 3;
  418. *e++ = 1; /* type field for UNDI */
  419. *e++ = 0; /* major revision */
  420. *e++ = 0; /* minor revision */
  421. uuid = getenv("pxeuuid");
  422. if (uuid) {
  423. if (uuid_str_valid(uuid)) {
  424. *e++ = 97; /* Client Machine Identifier */
  425. *e++ = 17;
  426. *e++ = 0; /* type 0 - UUID */
  427. uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
  428. e += 16;
  429. } else {
  430. printf("Invalid pxeuuid: %s\n", uuid);
  431. }
  432. }
  433. #endif
  434. #ifdef CONFIG_BOOTP_VCI_STRING
  435. put_vci(e, CONFIG_BOOTP_VCI_STRING);
  436. #endif
  437. #if defined(CONFIG_BOOTP_VENDOREX)
  438. x = dhcp_vendorex_prep(e);
  439. if (x)
  440. return x - start;
  441. #endif
  442. *e++ = 55; /* Parameter Request List */
  443. cnt = e++; /* Pointer to count of requested items */
  444. *cnt = 0;
  445. #if defined(CONFIG_BOOTP_SUBNETMASK)
  446. *e++ = 1; /* Subnet Mask */
  447. *cnt += 1;
  448. #endif
  449. #if defined(CONFIG_BOOTP_TIMEOFFSET)
  450. *e++ = 2;
  451. *cnt += 1;
  452. #endif
  453. #if defined(CONFIG_BOOTP_GATEWAY)
  454. *e++ = 3; /* Router Option */
  455. *cnt += 1;
  456. #endif
  457. #if defined(CONFIG_BOOTP_DNS)
  458. *e++ = 6; /* DNS Server(s) */
  459. *cnt += 1;
  460. #endif
  461. #if defined(CONFIG_BOOTP_HOSTNAME)
  462. *e++ = 12; /* Hostname */
  463. *cnt += 1;
  464. #endif
  465. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  466. *e++ = 13; /* Boot File Size */
  467. *cnt += 1;
  468. #endif
  469. #if defined(CONFIG_BOOTP_BOOTPATH)
  470. *e++ = 17; /* Boot path */
  471. *cnt += 1;
  472. #endif
  473. #if defined(CONFIG_BOOTP_NISDOMAIN)
  474. *e++ = 40; /* NIS Domain name request */
  475. *cnt += 1;
  476. #endif
  477. #if defined(CONFIG_BOOTP_NTPSERVER)
  478. *e++ = 42;
  479. *cnt += 1;
  480. #endif
  481. /* no options, so back up to avoid sending an empty request list */
  482. if (*cnt == 0)
  483. e -= 2;
  484. *e++ = 255; /* End of the list */
  485. /* Pad to minimal length */
  486. #ifdef CONFIG_DHCP_MIN_EXT_LEN
  487. while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
  488. *e++ = 0;
  489. #endif
  490. return e - start;
  491. }
  492. #else
  493. /*
  494. * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
  495. */
  496. static int bootp_extended(u8 *e)
  497. {
  498. u8 *start = e;
  499. *e++ = 99; /* RFC1048 Magic Cookie */
  500. *e++ = 130;
  501. *e++ = 83;
  502. *e++ = 99;
  503. #if defined(CONFIG_CMD_DHCP)
  504. *e++ = 53; /* DHCP Message Type */
  505. *e++ = 1;
  506. *e++ = DHCP_DISCOVER;
  507. *e++ = 57; /* Maximum DHCP Message Size */
  508. *e++ = 2;
  509. *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
  510. *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
  511. #endif
  512. #if defined(CONFIG_BOOTP_VCI_STRING) || \
  513. (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
  514. #ifdef CONFIG_SPL_BUILD
  515. put_vci(e, CONFIG_SPL_NET_VCI_STRING);
  516. #else
  517. put_vci(e, CONFIG_BOOTP_VCI_STRING);
  518. #endif
  519. #endif
  520. #if defined(CONFIG_BOOTP_SUBNETMASK)
  521. *e++ = 1; /* Subnet mask request */
  522. *e++ = 4;
  523. e += 4;
  524. #endif
  525. #if defined(CONFIG_BOOTP_GATEWAY)
  526. *e++ = 3; /* Default gateway request */
  527. *e++ = 4;
  528. e += 4;
  529. #endif
  530. #if defined(CONFIG_BOOTP_DNS)
  531. *e++ = 6; /* Domain Name Server */
  532. *e++ = 4;
  533. e += 4;
  534. #endif
  535. #if defined(CONFIG_BOOTP_HOSTNAME)
  536. *e++ = 12; /* Host name request */
  537. *e++ = 32;
  538. e += 32;
  539. #endif
  540. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  541. *e++ = 13; /* Boot file size */
  542. *e++ = 2;
  543. e += 2;
  544. #endif
  545. #if defined(CONFIG_BOOTP_BOOTPATH)
  546. *e++ = 17; /* Boot path */
  547. *e++ = 32;
  548. e += 32;
  549. #endif
  550. #if defined(CONFIG_BOOTP_NISDOMAIN)
  551. *e++ = 40; /* NIS Domain name request */
  552. *e++ = 32;
  553. e += 32;
  554. #endif
  555. #if defined(CONFIG_BOOTP_NTPSERVER)
  556. *e++ = 42;
  557. *e++ = 4;
  558. e += 4;
  559. #endif
  560. *e++ = 255; /* End of the list */
  561. return e - start;
  562. }
  563. #endif
  564. void bootp_reset(void)
  565. {
  566. bootp_num_ids = 0;
  567. bootp_try = 0;
  568. bootp_start = get_timer(0);
  569. bootp_timeout = 250;
  570. }
  571. void bootp_request(void)
  572. {
  573. uchar *pkt, *iphdr;
  574. struct bootp_hdr *bp;
  575. int extlen, pktlen, iplen;
  576. int eth_hdr_size;
  577. #ifdef CONFIG_BOOTP_RANDOM_DELAY
  578. ulong rand_ms;
  579. #endif
  580. ulong bootp_id;
  581. struct in_addr zero_ip;
  582. struct in_addr bcast_ip;
  583. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
  584. #if defined(CONFIG_CMD_DHCP)
  585. dhcp_state = INIT;
  586. #endif
  587. #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
  588. if (bootp_try == 0)
  589. srand_mac();
  590. if (bootp_try <= 2) /* Start with max 1024 * 1ms */
  591. rand_ms = rand() >> (22 - bootp_try);
  592. else /* After 3rd BOOTP request max 8192 * 1ms */
  593. rand_ms = rand() >> 19;
  594. printf("Random delay: %ld ms...\n", rand_ms);
  595. mdelay(rand_ms);
  596. #endif /* CONFIG_BOOTP_RANDOM_DELAY */
  597. printf("BOOTP broadcast %d\n", ++bootp_try);
  598. pkt = net_tx_packet;
  599. memset((void *)pkt, 0, PKTSIZE);
  600. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
  601. pkt += eth_hdr_size;
  602. /*
  603. * Next line results in incorrect packet size being transmitted,
  604. * resulting in errors in some DHCP servers, reporting missing bytes.
  605. * Size must be set in packet header after extension length has been
  606. * determined.
  607. * C. Hallinan, DS4.COM, Inc.
  608. */
  609. /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
  610. sizeof (struct bootp_hdr)); */
  611. iphdr = pkt; /* We need this later for net_set_udp_header() */
  612. pkt += IP_UDP_HDR_SIZE;
  613. bp = (struct bootp_hdr *)pkt;
  614. bp->bp_op = OP_BOOTREQUEST;
  615. bp->bp_htype = HWT_ETHER;
  616. bp->bp_hlen = HWL_ETHER;
  617. bp->bp_hops = 0;
  618. bp->bp_secs = htons(get_timer(0) / 1000);
  619. zero_ip.s_addr = 0;
  620. net_write_ip(&bp->bp_ciaddr, zero_ip);
  621. net_write_ip(&bp->bp_yiaddr, zero_ip);
  622. net_write_ip(&bp->bp_siaddr, zero_ip);
  623. net_write_ip(&bp->bp_giaddr, zero_ip);
  624. memcpy(bp->bp_chaddr, net_ethaddr, 6);
  625. copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
  626. /* Request additional information from the BOOTP/DHCP server */
  627. #if defined(CONFIG_CMD_DHCP)
  628. extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
  629. zero_ip);
  630. #else
  631. extlen = bootp_extended((u8 *)bp->bp_vend);
  632. #endif
  633. /*
  634. * Bootp ID is the lower 4 bytes of our ethernet address
  635. * plus the current time in ms.
  636. */
  637. bootp_id = ((ulong)net_ethaddr[2] << 24)
  638. | ((ulong)net_ethaddr[3] << 16)
  639. | ((ulong)net_ethaddr[4] << 8)
  640. | (ulong)net_ethaddr[5];
  641. bootp_id += get_timer(0);
  642. bootp_id = htonl(bootp_id);
  643. bootp_add_id(bootp_id);
  644. net_copy_long(&bp->bp_id, &bootp_id);
  645. /*
  646. * Calculate proper packet lengths taking into account the
  647. * variable size of the options field
  648. */
  649. iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
  650. pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
  651. bcast_ip.s_addr = 0xFFFFFFFFL;
  652. net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
  653. net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
  654. #if defined(CONFIG_CMD_DHCP)
  655. dhcp_state = SELECTING;
  656. net_set_udp_handler(dhcp_handler);
  657. #else
  658. net_set_udp_handler(bootp_handler);
  659. #endif
  660. net_send_packet(net_tx_packet, pktlen);
  661. }
  662. #if defined(CONFIG_CMD_DHCP)
  663. static void dhcp_process_options(uchar *popt, struct bootp_hdr *bp)
  664. {
  665. uchar *end = popt + BOOTP_HDR_SIZE;
  666. int oplen, size;
  667. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  668. int *to_ptr;
  669. #endif
  670. while (popt < end && *popt != 0xff) {
  671. oplen = *(popt + 1);
  672. switch (*popt) {
  673. case 1:
  674. net_copy_ip(&net_netmask, (popt + 2));
  675. break;
  676. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  677. case 2: /* Time offset */
  678. to_ptr = &net_ntp_time_offset;
  679. net_copy_long((ulong *)to_ptr, (ulong *)(popt + 2));
  680. net_ntp_time_offset = ntohl(net_ntp_time_offset);
  681. break;
  682. #endif
  683. case 3:
  684. net_copy_ip(&net_gateway, (popt + 2));
  685. break;
  686. case 6:
  687. net_copy_ip(&net_dns_server, (popt + 2));
  688. #if defined(CONFIG_BOOTP_DNS2)
  689. if (*(popt + 1) > 4)
  690. net_copy_ip(&net_dns_server2, (popt + 2 + 4));
  691. #endif
  692. break;
  693. case 12:
  694. size = truncate_sz("Host Name",
  695. sizeof(net_hostname), oplen);
  696. memcpy(&net_hostname, popt + 2, size);
  697. net_hostname[size] = 0;
  698. break;
  699. case 15: /* Ignore Domain Name Option */
  700. break;
  701. case 17:
  702. size = truncate_sz("Root Path",
  703. sizeof(net_root_path), oplen);
  704. memcpy(&net_root_path, popt + 2, size);
  705. net_root_path[size] = 0;
  706. break;
  707. case 28: /* Ignore Broadcast Address Option */
  708. break;
  709. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  710. case 42: /* NTP server IP */
  711. net_copy_ip(&net_ntp_server, (popt + 2));
  712. break;
  713. #endif
  714. case 51:
  715. net_copy_long(&dhcp_leasetime, (ulong *)(popt + 2));
  716. break;
  717. case 53: /* Ignore Message Type Option */
  718. break;
  719. case 54:
  720. net_copy_ip(&dhcp_server_ip, (popt + 2));
  721. break;
  722. case 58: /* Ignore Renewal Time Option */
  723. break;
  724. case 59: /* Ignore Rebinding Time Option */
  725. break;
  726. case 66: /* Ignore TFTP server name */
  727. break;
  728. case 67: /* vendor opt bootfile */
  729. /*
  730. * I can't use dhcp_vendorex_proc here because I need
  731. * to write into the bootp packet - even then I had to
  732. * pass the bootp packet pointer into here as the
  733. * second arg
  734. */
  735. size = truncate_sz("Opt Boot File",
  736. sizeof(bp->bp_file),
  737. oplen);
  738. if (bp->bp_file[0] == '\0' && size > 0) {
  739. /*
  740. * only use vendor boot file if we didn't
  741. * receive a boot file in the main non-vendor
  742. * part of the packet - god only knows why
  743. * some vendors chose not to use this perfectly
  744. * good spot to store the boot file (join on
  745. * Tru64 Unix) it seems mind bogglingly crazy
  746. * to me
  747. */
  748. printf("*** WARNING: using vendor "
  749. "optional boot file\n");
  750. memcpy(bp->bp_file, popt + 2, size);
  751. bp->bp_file[size] = '\0';
  752. }
  753. break;
  754. default:
  755. #if defined(CONFIG_BOOTP_VENDOREX)
  756. if (dhcp_vendorex_proc(popt))
  757. break;
  758. #endif
  759. printf("*** Unhandled DHCP Option in OFFER/ACK:"
  760. " %d\n", *popt);
  761. break;
  762. }
  763. popt += oplen + 2; /* Process next option */
  764. }
  765. }
  766. static int dhcp_message_type(unsigned char *popt)
  767. {
  768. if (net_read_long((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
  769. return -1;
  770. popt += 4;
  771. while (*popt != 0xff) {
  772. if (*popt == 53) /* DHCP Message Type */
  773. return *(popt + 2);
  774. popt += *(popt + 1) + 2; /* Scan through all options */
  775. }
  776. return -1;
  777. }
  778. static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
  779. {
  780. uchar *pkt, *iphdr;
  781. struct bootp_hdr *bp;
  782. int pktlen, iplen, extlen;
  783. int eth_hdr_size;
  784. struct in_addr offered_ip;
  785. struct in_addr zero_ip;
  786. struct in_addr bcast_ip;
  787. debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
  788. pkt = net_tx_packet;
  789. memset((void *)pkt, 0, PKTSIZE);
  790. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
  791. pkt += eth_hdr_size;
  792. iphdr = pkt; /* We'll need this later to set proper pkt size */
  793. pkt += IP_UDP_HDR_SIZE;
  794. bp = (struct bootp_hdr *)pkt;
  795. bp->bp_op = OP_BOOTREQUEST;
  796. bp->bp_htype = HWT_ETHER;
  797. bp->bp_hlen = HWL_ETHER;
  798. bp->bp_hops = 0;
  799. bp->bp_secs = htons(get_timer(0) / 1000);
  800. /* Do not set the client IP, your IP, or server IP yet, since it
  801. * hasn't been ACK'ed by the server yet */
  802. /*
  803. * RFC3046 requires Relay Agents to discard packets with
  804. * nonzero and offered giaddr
  805. */
  806. zero_ip.s_addr = 0;
  807. net_write_ip(&bp->bp_giaddr, zero_ip);
  808. memcpy(bp->bp_chaddr, net_ethaddr, 6);
  809. /*
  810. * ID is the id of the OFFER packet
  811. */
  812. net_copy_long(&bp->bp_id, &bp_offer->bp_id);
  813. /*
  814. * Copy options from OFFER packet if present
  815. */
  816. /* Copy offered IP into the parameters request list */
  817. net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
  818. extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
  819. dhcp_server_ip, offered_ip);
  820. iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
  821. pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
  822. bcast_ip.s_addr = 0xFFFFFFFFL;
  823. net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
  824. #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
  825. udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
  826. #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
  827. debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
  828. net_send_packet(net_tx_packet, pktlen);
  829. }
  830. /*
  831. * Handle DHCP received packets.
  832. */
  833. static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  834. unsigned src, unsigned len)
  835. {
  836. struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
  837. debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
  838. src, dest, len, dhcp_state);
  839. /* Filter out pkts we don't want */
  840. if (check_packet(pkt, dest, src, len))
  841. return;
  842. debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
  843. "%d\n", src, dest, len, dhcp_state);
  844. switch (dhcp_state) {
  845. case SELECTING:
  846. /*
  847. * Wait an appropriate time for any potential DHCPOFFER packets
  848. * to arrive. Then select one, and generate DHCPREQUEST
  849. * response. If filename is in format we recognize, assume it
  850. * is a valid OFFER from a server we want.
  851. */
  852. debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
  853. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  854. if (strncmp(bp->bp_file,
  855. CONFIG_SYS_BOOTFILE_PREFIX,
  856. strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
  857. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  858. debug("TRANSITIONING TO REQUESTING STATE\n");
  859. dhcp_state = REQUESTING;
  860. if (net_read_long((ulong *)&bp->bp_vend[0]) ==
  861. htonl(BOOTP_VENDOR_MAGIC))
  862. dhcp_process_options((u8 *)&bp->bp_vend[4], bp);
  863. net_set_timeout_handler(5000, bootp_timeout_handler);
  864. dhcp_send_request_packet(bp);
  865. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  866. }
  867. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  868. return;
  869. break;
  870. case REQUESTING:
  871. debug("DHCP State: REQUESTING\n");
  872. if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
  873. if (net_read_long((ulong *)&bp->bp_vend[0]) ==
  874. htonl(BOOTP_VENDOR_MAGIC))
  875. dhcp_process_options((u8 *)&bp->bp_vend[4], bp);
  876. /* Store net params from reply */
  877. store_net_params(bp);
  878. dhcp_state = BOUND;
  879. printf("DHCP client bound to address %pI4 (%lu ms)\n",
  880. &net_ip, get_timer(bootp_start));
  881. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
  882. "bootp_stop");
  883. net_auto_load();
  884. return;
  885. }
  886. break;
  887. case BOUND:
  888. /* DHCP client bound to address */
  889. break;
  890. default:
  891. puts("DHCP: INVALID STATE\n");
  892. break;
  893. }
  894. }
  895. void dhcp_request(void)
  896. {
  897. bootp_request();
  898. }
  899. #endif /* CONFIG_CMD_DHCP */