bootp.c 25 KB

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