bootp.c 27 KB

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