bootp.c 26 KB

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