bootp.c 26 KB

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