bootp.c 26 KB

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