bootp.c 27 KB

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