bootp.c 26 KB

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