bootp.c 25 KB

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