bootp.c 27 KB

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