bootp.c 26 KB

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