bootp.c 25 KB

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