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. static u8 *add_vci(u8 *e)
  361. {
  362. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
  363. put_vci(e, CONFIG_SPL_NET_VCI_STRING);
  364. #elif defined(CONFIG_BOOTP_VCI_STRING)
  365. put_vci(e, CONFIG_BOOTP_VCI_STRING);
  366. #endif
  367. return e;
  368. }
  369. /*
  370. * Initialize BOOTP extension fields in the request.
  371. */
  372. #if defined(CONFIG_CMD_DHCP)
  373. static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
  374. struct in_addr requested_ip)
  375. {
  376. u8 *start = e;
  377. u8 *cnt;
  378. #if defined(CONFIG_BOOTP_PXE)
  379. char *uuid;
  380. u16 clientarch;
  381. #endif
  382. #if defined(CONFIG_BOOTP_VENDOREX)
  383. u8 *x;
  384. #endif
  385. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  386. char *hostname;
  387. #endif
  388. *e++ = 99; /* RFC1048 Magic Cookie */
  389. *e++ = 130;
  390. *e++ = 83;
  391. *e++ = 99;
  392. *e++ = 53; /* DHCP Message Type */
  393. *e++ = 1;
  394. *e++ = message_type;
  395. *e++ = 57; /* Maximum DHCP Message Size */
  396. *e++ = 2;
  397. *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
  398. *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
  399. if (server_ip.s_addr) {
  400. int tmp = ntohl(server_ip.s_addr);
  401. *e++ = 54; /* ServerID */
  402. *e++ = 4;
  403. *e++ = tmp >> 24;
  404. *e++ = tmp >> 16;
  405. *e++ = tmp >> 8;
  406. *e++ = tmp & 0xff;
  407. }
  408. if (requested_ip.s_addr) {
  409. int tmp = ntohl(requested_ip.s_addr);
  410. *e++ = 50; /* Requested IP */
  411. *e++ = 4;
  412. *e++ = tmp >> 24;
  413. *e++ = tmp >> 16;
  414. *e++ = tmp >> 8;
  415. *e++ = tmp & 0xff;
  416. }
  417. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  418. hostname = getenv("hostname");
  419. if (hostname) {
  420. int hostnamelen = strlen(hostname);
  421. *e++ = 12; /* Hostname */
  422. *e++ = hostnamelen;
  423. memcpy(e, hostname, hostnamelen);
  424. e += hostnamelen;
  425. }
  426. #endif
  427. #if defined(CONFIG_BOOTP_PXE)
  428. clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
  429. *e++ = 93; /* Client System Architecture */
  430. *e++ = 2;
  431. *e++ = (clientarch >> 8) & 0xff;
  432. *e++ = clientarch & 0xff;
  433. *e++ = 94; /* Client Network Interface Identifier */
  434. *e++ = 3;
  435. *e++ = 1; /* type field for UNDI */
  436. *e++ = 0; /* major revision */
  437. *e++ = 0; /* minor revision */
  438. uuid = getenv("pxeuuid");
  439. if (uuid) {
  440. if (uuid_str_valid(uuid)) {
  441. *e++ = 97; /* Client Machine Identifier */
  442. *e++ = 17;
  443. *e++ = 0; /* type 0 - UUID */
  444. uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
  445. e += 16;
  446. } else {
  447. printf("Invalid pxeuuid: %s\n", uuid);
  448. }
  449. }
  450. #endif
  451. e = add_vci(e);
  452. #if defined(CONFIG_BOOTP_VENDOREX)
  453. x = dhcp_vendorex_prep(e);
  454. if (x)
  455. return x - start;
  456. #endif
  457. *e++ = 55; /* Parameter Request List */
  458. cnt = e++; /* Pointer to count of requested items */
  459. *cnt = 0;
  460. #if defined(CONFIG_BOOTP_SUBNETMASK)
  461. *e++ = 1; /* Subnet Mask */
  462. *cnt += 1;
  463. #endif
  464. #if defined(CONFIG_BOOTP_TIMEOFFSET)
  465. *e++ = 2;
  466. *cnt += 1;
  467. #endif
  468. #if defined(CONFIG_BOOTP_GATEWAY)
  469. *e++ = 3; /* Router Option */
  470. *cnt += 1;
  471. #endif
  472. #if defined(CONFIG_BOOTP_DNS)
  473. *e++ = 6; /* DNS Server(s) */
  474. *cnt += 1;
  475. #endif
  476. #if defined(CONFIG_BOOTP_HOSTNAME)
  477. *e++ = 12; /* Hostname */
  478. *cnt += 1;
  479. #endif
  480. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  481. *e++ = 13; /* Boot File Size */
  482. *cnt += 1;
  483. #endif
  484. #if defined(CONFIG_BOOTP_BOOTPATH)
  485. *e++ = 17; /* Boot path */
  486. *cnt += 1;
  487. #endif
  488. #if defined(CONFIG_BOOTP_NISDOMAIN)
  489. *e++ = 40; /* NIS Domain name request */
  490. *cnt += 1;
  491. #endif
  492. #if defined(CONFIG_BOOTP_NTPSERVER)
  493. *e++ = 42;
  494. *cnt += 1;
  495. #endif
  496. /* no options, so back up to avoid sending an empty request list */
  497. if (*cnt == 0)
  498. e -= 2;
  499. *e++ = 255; /* End of the list */
  500. /* Pad to minimal length */
  501. #ifdef CONFIG_DHCP_MIN_EXT_LEN
  502. while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
  503. *e++ = 0;
  504. #endif
  505. return e - start;
  506. }
  507. #else
  508. /*
  509. * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
  510. */
  511. static int bootp_extended(u8 *e)
  512. {
  513. u8 *start = e;
  514. *e++ = 99; /* RFC1048 Magic Cookie */
  515. *e++ = 130;
  516. *e++ = 83;
  517. *e++ = 99;
  518. #if defined(CONFIG_CMD_DHCP)
  519. *e++ = 53; /* DHCP Message Type */
  520. *e++ = 1;
  521. *e++ = DHCP_DISCOVER;
  522. *e++ = 57; /* Maximum DHCP Message Size */
  523. *e++ = 2;
  524. *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
  525. *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
  526. #endif
  527. add_vci(e);
  528. #if defined(CONFIG_BOOTP_SUBNETMASK)
  529. *e++ = 1; /* Subnet mask request */
  530. *e++ = 4;
  531. e += 4;
  532. #endif
  533. #if defined(CONFIG_BOOTP_GATEWAY)
  534. *e++ = 3; /* Default gateway request */
  535. *e++ = 4;
  536. e += 4;
  537. #endif
  538. #if defined(CONFIG_BOOTP_DNS)
  539. *e++ = 6; /* Domain Name Server */
  540. *e++ = 4;
  541. e += 4;
  542. #endif
  543. #if defined(CONFIG_BOOTP_HOSTNAME)
  544. *e++ = 12; /* Host name request */
  545. *e++ = 32;
  546. e += 32;
  547. #endif
  548. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  549. *e++ = 13; /* Boot file size */
  550. *e++ = 2;
  551. e += 2;
  552. #endif
  553. #if defined(CONFIG_BOOTP_BOOTPATH)
  554. *e++ = 17; /* Boot path */
  555. *e++ = 32;
  556. e += 32;
  557. #endif
  558. #if defined(CONFIG_BOOTP_NISDOMAIN)
  559. *e++ = 40; /* NIS Domain name request */
  560. *e++ = 32;
  561. e += 32;
  562. #endif
  563. #if defined(CONFIG_BOOTP_NTPSERVER)
  564. *e++ = 42;
  565. *e++ = 4;
  566. e += 4;
  567. #endif
  568. *e++ = 255; /* End of the list */
  569. return e - start;
  570. }
  571. #endif
  572. void bootp_reset(void)
  573. {
  574. bootp_num_ids = 0;
  575. bootp_try = 0;
  576. bootp_start = get_timer(0);
  577. bootp_timeout = 250;
  578. }
  579. void bootp_request(void)
  580. {
  581. uchar *pkt, *iphdr;
  582. struct bootp_hdr *bp;
  583. int extlen, pktlen, iplen;
  584. int eth_hdr_size;
  585. #ifdef CONFIG_BOOTP_RANDOM_DELAY
  586. ulong rand_ms;
  587. #endif
  588. u32 bootp_id;
  589. struct in_addr zero_ip;
  590. struct in_addr bcast_ip;
  591. char *ep; /* Environment pointer */
  592. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
  593. #if defined(CONFIG_CMD_DHCP)
  594. dhcp_state = INIT;
  595. #endif
  596. ep = getenv("bootpretryperiod");
  597. if (ep != NULL)
  598. time_taken_max = simple_strtoul(ep, NULL, 10);
  599. else
  600. time_taken_max = TIMEOUT_MS;
  601. #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
  602. if (bootp_try == 0)
  603. srand_mac();
  604. if (bootp_try <= 2) /* Start with max 1024 * 1ms */
  605. rand_ms = rand() >> (22 - bootp_try);
  606. else /* After 3rd BOOTP request max 8192 * 1ms */
  607. rand_ms = rand() >> 19;
  608. printf("Random delay: %ld ms...\n", rand_ms);
  609. mdelay(rand_ms);
  610. #endif /* CONFIG_BOOTP_RANDOM_DELAY */
  611. printf("BOOTP broadcast %d\n", ++bootp_try);
  612. pkt = net_tx_packet;
  613. memset((void *)pkt, 0, PKTSIZE);
  614. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
  615. pkt += eth_hdr_size;
  616. /*
  617. * Next line results in incorrect packet size being transmitted,
  618. * resulting in errors in some DHCP servers, reporting missing bytes.
  619. * Size must be set in packet header after extension length has been
  620. * determined.
  621. * C. Hallinan, DS4.COM, Inc.
  622. */
  623. /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
  624. sizeof (struct bootp_hdr)); */
  625. iphdr = pkt; /* We need this later for net_set_udp_header() */
  626. pkt += IP_UDP_HDR_SIZE;
  627. bp = (struct bootp_hdr *)pkt;
  628. bp->bp_op = OP_BOOTREQUEST;
  629. bp->bp_htype = HWT_ETHER;
  630. bp->bp_hlen = HWL_ETHER;
  631. bp->bp_hops = 0;
  632. /*
  633. * according to RFC1542, should be 0 on first request, secs since
  634. * first request otherwise
  635. */
  636. bp->bp_secs = htons(get_timer(bootp_start) / 1000);
  637. zero_ip.s_addr = 0;
  638. net_write_ip(&bp->bp_ciaddr, zero_ip);
  639. net_write_ip(&bp->bp_yiaddr, zero_ip);
  640. net_write_ip(&bp->bp_siaddr, zero_ip);
  641. net_write_ip(&bp->bp_giaddr, zero_ip);
  642. memcpy(bp->bp_chaddr, net_ethaddr, 6);
  643. copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
  644. /* Request additional information from the BOOTP/DHCP server */
  645. #if defined(CONFIG_CMD_DHCP)
  646. extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
  647. zero_ip);
  648. #else
  649. extlen = bootp_extended((u8 *)bp->bp_vend);
  650. #endif
  651. /*
  652. * Bootp ID is the lower 4 bytes of our ethernet address
  653. * plus the current time in ms.
  654. */
  655. bootp_id = ((u32)net_ethaddr[2] << 24)
  656. | ((u32)net_ethaddr[3] << 16)
  657. | ((u32)net_ethaddr[4] << 8)
  658. | (u32)net_ethaddr[5];
  659. bootp_id += get_timer(0);
  660. bootp_id = htonl(bootp_id);
  661. bootp_add_id(bootp_id);
  662. net_copy_u32(&bp->bp_id, &bootp_id);
  663. /*
  664. * Calculate proper packet lengths taking into account the
  665. * variable size of the options field
  666. */
  667. iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
  668. pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
  669. bcast_ip.s_addr = 0xFFFFFFFFL;
  670. net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
  671. net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
  672. #if defined(CONFIG_CMD_DHCP)
  673. dhcp_state = SELECTING;
  674. net_set_udp_handler(dhcp_handler);
  675. #else
  676. net_set_udp_handler(bootp_handler);
  677. #endif
  678. net_send_packet(net_tx_packet, pktlen);
  679. }
  680. #if defined(CONFIG_CMD_DHCP)
  681. static void dhcp_process_options(uchar *popt, uchar *end)
  682. {
  683. int oplen, size;
  684. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  685. int *to_ptr;
  686. #endif
  687. while (popt < end && *popt != 0xff) {
  688. oplen = *(popt + 1);
  689. switch (*popt) {
  690. case 0:
  691. oplen = -1; /* Pad omits len byte */
  692. break;
  693. case 1:
  694. net_copy_ip(&net_netmask, (popt + 2));
  695. break;
  696. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  697. case 2: /* Time offset */
  698. to_ptr = &net_ntp_time_offset;
  699. net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
  700. net_ntp_time_offset = ntohl(net_ntp_time_offset);
  701. break;
  702. #endif
  703. case 3:
  704. net_copy_ip(&net_gateway, (popt + 2));
  705. break;
  706. case 6:
  707. net_copy_ip(&net_dns_server, (popt + 2));
  708. #if defined(CONFIG_BOOTP_DNS2)
  709. if (*(popt + 1) > 4)
  710. net_copy_ip(&net_dns_server2, (popt + 2 + 4));
  711. #endif
  712. break;
  713. case 12:
  714. size = truncate_sz("Host Name",
  715. sizeof(net_hostname), oplen);
  716. memcpy(&net_hostname, popt + 2, size);
  717. net_hostname[size] = 0;
  718. break;
  719. case 15: /* Ignore Domain Name Option */
  720. break;
  721. case 17:
  722. size = truncate_sz("Root Path",
  723. sizeof(net_root_path), oplen);
  724. memcpy(&net_root_path, popt + 2, size);
  725. net_root_path[size] = 0;
  726. break;
  727. case 28: /* Ignore Broadcast Address Option */
  728. break;
  729. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  730. case 42: /* NTP server IP */
  731. net_copy_ip(&net_ntp_server, (popt + 2));
  732. break;
  733. #endif
  734. case 51:
  735. net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
  736. break;
  737. case 52:
  738. dhcp_option_overload = popt[2];
  739. break;
  740. case 53: /* Ignore Message Type Option */
  741. break;
  742. case 54:
  743. net_copy_ip(&dhcp_server_ip, (popt + 2));
  744. break;
  745. case 58: /* Ignore Renewal Time Option */
  746. break;
  747. case 59: /* Ignore Rebinding Time Option */
  748. break;
  749. case 66: /* Ignore TFTP server name */
  750. break;
  751. case 67: /* Bootfile option */
  752. size = truncate_sz("Bootfile",
  753. sizeof(net_boot_file_name), oplen);
  754. memcpy(&net_boot_file_name, popt + 2, size);
  755. net_boot_file_name[size] = 0;
  756. break;
  757. default:
  758. #if defined(CONFIG_BOOTP_VENDOREX)
  759. if (dhcp_vendorex_proc(popt))
  760. break;
  761. #endif
  762. printf("*** Unhandled DHCP Option in OFFER/ACK:"
  763. " %d\n", *popt);
  764. break;
  765. }
  766. popt += oplen + 2; /* Process next option */
  767. }
  768. }
  769. static void dhcp_packet_process_options(struct bootp_hdr *bp)
  770. {
  771. uchar *popt = (uchar *)&bp->bp_vend[4];
  772. uchar *end = popt + BOOTP_HDR_SIZE;
  773. if (net_read_u32((u32 *)&bp->bp_vend[0]) != htonl(BOOTP_VENDOR_MAGIC))
  774. return;
  775. dhcp_option_overload = 0;
  776. /*
  777. * The 'options' field MUST be interpreted first, 'file' next,
  778. * 'sname' last.
  779. */
  780. dhcp_process_options(popt, end);
  781. if (dhcp_option_overload & OVERLOAD_FILE) {
  782. popt = (uchar *)bp->bp_file;
  783. end = popt + sizeof(bp->bp_file);
  784. dhcp_process_options(popt, end);
  785. }
  786. if (dhcp_option_overload & OVERLOAD_SNAME) {
  787. popt = (uchar *)bp->bp_sname;
  788. end = popt + sizeof(bp->bp_sname);
  789. dhcp_process_options(popt, end);
  790. }
  791. }
  792. static int dhcp_message_type(unsigned char *popt)
  793. {
  794. if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
  795. return -1;
  796. popt += 4;
  797. while (*popt != 0xff) {
  798. if (*popt == 53) /* DHCP Message Type */
  799. return *(popt + 2);
  800. if (*popt == 0) {
  801. /* Pad */
  802. popt += 1;
  803. } else {
  804. /* Scan through all options */
  805. popt += *(popt + 1) + 2;
  806. }
  807. }
  808. return -1;
  809. }
  810. static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
  811. {
  812. uchar *pkt, *iphdr;
  813. struct bootp_hdr *bp;
  814. int pktlen, iplen, extlen;
  815. int eth_hdr_size;
  816. struct in_addr offered_ip;
  817. struct in_addr zero_ip;
  818. struct in_addr bcast_ip;
  819. debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
  820. pkt = net_tx_packet;
  821. memset((void *)pkt, 0, PKTSIZE);
  822. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
  823. pkt += eth_hdr_size;
  824. iphdr = pkt; /* We'll need this later to set proper pkt size */
  825. pkt += IP_UDP_HDR_SIZE;
  826. bp = (struct bootp_hdr *)pkt;
  827. bp->bp_op = OP_BOOTREQUEST;
  828. bp->bp_htype = HWT_ETHER;
  829. bp->bp_hlen = HWL_ETHER;
  830. bp->bp_hops = 0;
  831. bp->bp_secs = htons(get_timer(bootp_start) / 1000);
  832. /* Do not set the client IP, your IP, or server IP yet, since it
  833. * hasn't been ACK'ed by the server yet */
  834. /*
  835. * RFC3046 requires Relay Agents to discard packets with
  836. * nonzero and offered giaddr
  837. */
  838. zero_ip.s_addr = 0;
  839. net_write_ip(&bp->bp_giaddr, zero_ip);
  840. memcpy(bp->bp_chaddr, net_ethaddr, 6);
  841. copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
  842. /*
  843. * ID is the id of the OFFER packet
  844. */
  845. net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
  846. /*
  847. * Copy options from OFFER packet if present
  848. */
  849. /* Copy offered IP into the parameters request list */
  850. net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
  851. extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
  852. dhcp_server_ip, offered_ip);
  853. iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
  854. pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
  855. bcast_ip.s_addr = 0xFFFFFFFFL;
  856. net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
  857. #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
  858. udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
  859. #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
  860. debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
  861. net_send_packet(net_tx_packet, pktlen);
  862. }
  863. /*
  864. * Handle DHCP received packets.
  865. */
  866. static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  867. unsigned src, unsigned len)
  868. {
  869. struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
  870. debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
  871. src, dest, len, dhcp_state);
  872. /* Filter out pkts we don't want */
  873. if (check_reply_packet(pkt, dest, src, len))
  874. return;
  875. debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
  876. "%d\n", src, dest, len, dhcp_state);
  877. if (net_read_ip(&bp->bp_yiaddr).s_addr == 0)
  878. return;
  879. switch (dhcp_state) {
  880. case SELECTING:
  881. /*
  882. * Wait an appropriate time for any potential DHCPOFFER packets
  883. * to arrive. Then select one, and generate DHCPREQUEST
  884. * response. If filename is in format we recognize, assume it
  885. * is a valid OFFER from a server we want.
  886. */
  887. debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
  888. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  889. if (strncmp(bp->bp_file,
  890. CONFIG_SYS_BOOTFILE_PREFIX,
  891. strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
  892. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  893. dhcp_packet_process_options(bp);
  894. efi_net_set_dhcp_ack(pkt, len);
  895. debug("TRANSITIONING TO REQUESTING STATE\n");
  896. dhcp_state = REQUESTING;
  897. net_set_timeout_handler(5000, bootp_timeout_handler);
  898. dhcp_send_request_packet(bp);
  899. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  900. }
  901. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  902. return;
  903. break;
  904. case REQUESTING:
  905. debug("DHCP State: REQUESTING\n");
  906. if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
  907. dhcp_packet_process_options(bp);
  908. /* Store net params from reply */
  909. store_net_params(bp);
  910. dhcp_state = BOUND;
  911. printf("DHCP client bound to address %pI4 (%lu ms)\n",
  912. &net_ip, get_timer(bootp_start));
  913. net_set_timeout_handler(0, (thand_f *)0);
  914. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
  915. "bootp_stop");
  916. net_auto_load();
  917. return;
  918. }
  919. break;
  920. case BOUND:
  921. /* DHCP client bound to address */
  922. break;
  923. default:
  924. puts("DHCP: INVALID STATE\n");
  925. break;
  926. }
  927. }
  928. void dhcp_request(void)
  929. {
  930. bootp_request();
  931. }
  932. #endif /* CONFIG_CMD_DHCP */