bootp.c 26 KB

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