bootp.c 27 KB

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