bootp.c 25 KB

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