bootp.c 27 KB

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