bootp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * Based on LiMon - BOOTP.
  3. *
  4. * Copyright 1994, 1995, 2000 Neil Russell.
  5. * (See License)
  6. * Copyright 2000 Roland Borde
  7. * Copyright 2000 Paolo Scaffardi
  8. * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <net.h>
  13. #include "bootp.h"
  14. #include "tftp.h"
  15. #include "nfs.h"
  16. #ifdef CONFIG_STATUS_LED
  17. #include <status_led.h>
  18. #endif
  19. #include <linux/compiler.h>
  20. #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
  21. #define TIMEOUT 5000UL /* Milliseconds before trying BOOTP again */
  22. #ifndef CONFIG_NET_RETRY_COUNT
  23. # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  24. #else
  25. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  26. #endif
  27. #define PORT_BOOTPS 67 /* BOOTP server UDP port */
  28. #define PORT_BOOTPC 68 /* BOOTP client UDP port */
  29. #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
  30. #define CONFIG_DHCP_MIN_EXT_LEN 64
  31. #endif
  32. ulong BootpID;
  33. int BootpTry;
  34. #ifdef CONFIG_BOOTP_RANDOM_DELAY
  35. ulong seed1, seed2;
  36. #endif
  37. #if defined(CONFIG_CMD_DHCP)
  38. dhcp_state_t dhcp_state = INIT;
  39. unsigned long dhcp_leasetime;
  40. IPaddr_t NetDHCPServerIP;
  41. static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  42. unsigned len);
  43. /* For Debug */
  44. #if 0
  45. static char *dhcpmsg2str(int type)
  46. {
  47. switch (type) {
  48. case 1: return "DHCPDISCOVER"; break;
  49. case 2: return "DHCPOFFER"; break;
  50. case 3: return "DHCPREQUEST"; break;
  51. case 4: return "DHCPDECLINE"; break;
  52. case 5: return "DHCPACK"; break;
  53. case 6: return "DHCPNACK"; break;
  54. case 7: return "DHCPRELEASE"; break;
  55. default: return "UNKNOWN/INVALID MSG TYPE"; break;
  56. }
  57. }
  58. #endif
  59. #endif
  60. static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  61. {
  62. struct Bootp_t *bp = (struct Bootp_t *) pkt;
  63. int retval = 0;
  64. if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
  65. retval = -1;
  66. else if (len < sizeof(struct Bootp_t) - OPT_SIZE)
  67. retval = -2;
  68. else if (bp->bp_op != OP_BOOTREQUEST &&
  69. bp->bp_op != OP_BOOTREPLY &&
  70. bp->bp_op != DHCP_OFFER &&
  71. bp->bp_op != DHCP_ACK &&
  72. bp->bp_op != DHCP_NAK)
  73. retval = -3;
  74. else if (bp->bp_htype != HWT_ETHER)
  75. retval = -4;
  76. else if (bp->bp_hlen != HWL_ETHER)
  77. retval = -5;
  78. else if (NetReadLong((ulong *)&bp->bp_id) != BootpID)
  79. retval = -6;
  80. debug("Filtering pkt = %d\n", retval);
  81. return retval;
  82. }
  83. /*
  84. * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
  85. */
  86. static void BootpCopyNetParams(struct Bootp_t *bp)
  87. {
  88. __maybe_unused IPaddr_t tmp_ip;
  89. NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
  90. #if !defined(CONFIG_BOOTP_SERVERIP)
  91. NetCopyIP(&tmp_ip, &bp->bp_siaddr);
  92. if (tmp_ip != 0)
  93. NetCopyIP(&NetServerIP, &bp->bp_siaddr);
  94. memcpy(NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
  95. #endif
  96. if (strlen(bp->bp_file) > 0)
  97. copy_filename(BootFile, bp->bp_file, sizeof(BootFile));
  98. debug("Bootfile: %s\n", BootFile);
  99. /* Propagate to environment:
  100. * don't delete exising entry when BOOTP / DHCP reply does
  101. * not contain a new value
  102. */
  103. if (*BootFile)
  104. setenv("bootfile", BootFile);
  105. }
  106. static int truncate_sz(const char *name, int maxlen, int curlen)
  107. {
  108. if (curlen >= maxlen) {
  109. printf("*** WARNING: %s is too long (%d - max: %d)"
  110. " - truncated\n", name, curlen, maxlen);
  111. curlen = maxlen - 1;
  112. }
  113. return curlen;
  114. }
  115. #if !defined(CONFIG_CMD_DHCP)
  116. static void BootpVendorFieldProcess(u8 *ext)
  117. {
  118. int size = *(ext + 1);
  119. debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
  120. *(ext + 1));
  121. NetBootFileSize = 0;
  122. switch (*ext) {
  123. /* Fixed length fields */
  124. case 1: /* Subnet mask */
  125. if (NetOurSubnetMask == 0)
  126. NetCopyIP(&NetOurSubnetMask, (IPaddr_t *) (ext + 2));
  127. break;
  128. case 2: /* Time offset - Not yet supported */
  129. break;
  130. /* Variable length fields */
  131. case 3: /* Gateways list */
  132. if (NetOurGatewayIP == 0)
  133. NetCopyIP(&NetOurGatewayIP, (IPaddr_t *) (ext + 2));
  134. break;
  135. case 4: /* Time server - Not yet supported */
  136. break;
  137. case 5: /* IEN-116 name server - Not yet supported */
  138. break;
  139. case 6:
  140. if (NetOurDNSIP == 0)
  141. NetCopyIP(&NetOurDNSIP, (IPaddr_t *) (ext + 2));
  142. #if defined(CONFIG_BOOTP_DNS2)
  143. if ((NetOurDNS2IP == 0) && (size > 4))
  144. NetCopyIP(&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
  145. #endif
  146. break;
  147. case 7: /* Log server - Not yet supported */
  148. break;
  149. case 8: /* Cookie/Quote server - Not yet supported */
  150. break;
  151. case 9: /* LPR server - Not yet supported */
  152. break;
  153. case 10: /* Impress server - Not yet supported */
  154. break;
  155. case 11: /* RPL server - Not yet supported */
  156. break;
  157. case 12: /* Host name */
  158. if (NetOurHostName[0] == 0) {
  159. size = truncate_sz("Host Name",
  160. sizeof(NetOurHostName), size);
  161. memcpy(&NetOurHostName, ext + 2, size);
  162. NetOurHostName[size] = 0;
  163. }
  164. break;
  165. case 13: /* Boot file size */
  166. if (size == 2)
  167. NetBootFileSize = ntohs(*(ushort *) (ext + 2));
  168. else if (size == 4)
  169. NetBootFileSize = ntohl(*(ulong *) (ext + 2));
  170. break;
  171. case 14: /* Merit dump file - Not yet supported */
  172. break;
  173. case 15: /* Domain name - Not yet supported */
  174. break;
  175. case 16: /* Swap server - Not yet supported */
  176. break;
  177. case 17: /* Root path */
  178. if (NetOurRootPath[0] == 0) {
  179. size = truncate_sz("Root Path",
  180. sizeof(NetOurRootPath), size);
  181. memcpy(&NetOurRootPath, ext + 2, size);
  182. NetOurRootPath[size] = 0;
  183. }
  184. break;
  185. case 18: /* Extension path - Not yet supported */
  186. /*
  187. * This can be used to send the information of the
  188. * vendor area in another file that the client can
  189. * access via TFTP.
  190. */
  191. break;
  192. /* IP host layer fields */
  193. case 40: /* NIS Domain name */
  194. if (NetOurNISDomain[0] == 0) {
  195. size = truncate_sz("NIS Domain Name",
  196. sizeof(NetOurNISDomain), size);
  197. memcpy(&NetOurNISDomain, ext + 2, size);
  198. NetOurNISDomain[size] = 0;
  199. }
  200. break;
  201. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  202. case 42: /* NTP server IP */
  203. NetCopyIP(&NetNtpServerIP, (IPaddr_t *) (ext + 2));
  204. break;
  205. #endif
  206. /* Application layer fields */
  207. case 43: /* Vendor specific info - Not yet supported */
  208. /*
  209. * Binary information to exchange specific
  210. * product information.
  211. */
  212. break;
  213. /* Reserved (custom) fields (128..254) */
  214. }
  215. }
  216. static void BootpVendorProcess(u8 *ext, int size)
  217. {
  218. u8 *end = ext + size;
  219. debug("[BOOTP] Checking extension (%d bytes)...\n", size);
  220. while ((ext < end) && (*ext != 0xff)) {
  221. if (*ext == 0) {
  222. ext++;
  223. } else {
  224. u8 *opt = ext;
  225. ext += ext[1] + 2;
  226. if (ext <= end)
  227. BootpVendorFieldProcess(opt);
  228. }
  229. }
  230. debug("[BOOTP] Received fields:\n");
  231. if (NetOurSubnetMask)
  232. debug("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
  233. if (NetOurGatewayIP)
  234. debug("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
  235. if (NetBootFileSize)
  236. debug("NetBootFileSize : %d\n", NetBootFileSize);
  237. if (NetOurHostName[0])
  238. debug("NetOurHostName : %s\n", NetOurHostName);
  239. if (NetOurRootPath[0])
  240. debug("NetOurRootPath : %s\n", NetOurRootPath);
  241. if (NetOurNISDomain[0])
  242. debug("NetOurNISDomain : %s\n", NetOurNISDomain);
  243. if (NetBootFileSize)
  244. debug("NetBootFileSize: %d\n", NetBootFileSize);
  245. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  246. if (NetNtpServerIP)
  247. debug("NetNtpServerIP : %pI4\n", &NetNtpServerIP);
  248. #endif
  249. }
  250. /*
  251. * Handle a BOOTP received packet.
  252. */
  253. static void
  254. BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  255. unsigned len)
  256. {
  257. struct Bootp_t *bp;
  258. debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
  259. src, dest, len, sizeof(struct Bootp_t));
  260. bp = (struct Bootp_t *)pkt;
  261. /* Filter out pkts we don't want */
  262. if (BootpCheckPkt(pkt, dest, src, len))
  263. return;
  264. /*
  265. * Got a good BOOTP reply. Copy the data into our variables.
  266. */
  267. #ifdef CONFIG_STATUS_LED
  268. status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
  269. #endif
  270. BootpCopyNetParams(bp); /* Store net parameters from reply */
  271. /* Retrieve extended information (we must parse the vendor area) */
  272. if (NetReadLong((ulong *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
  273. BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
  274. NetSetTimeout(0, (thand_f *)0);
  275. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
  276. debug("Got good BOOTP\n");
  277. net_auto_load();
  278. }
  279. #endif
  280. /*
  281. * Timeout on BOOTP/DHCP request.
  282. */
  283. static void
  284. BootpTimeout(void)
  285. {
  286. if (BootpTry >= TIMEOUT_COUNT) {
  287. puts("\nRetry count exceeded; starting again\n");
  288. NetStartAgain();
  289. } else {
  290. NetSetTimeout(TIMEOUT, BootpTimeout);
  291. BootpRequest();
  292. }
  293. }
  294. /*
  295. * Initialize BOOTP extension fields in the request.
  296. */
  297. #if defined(CONFIG_CMD_DHCP)
  298. static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
  299. IPaddr_t RequestedIP)
  300. {
  301. u8 *start = e;
  302. u8 *cnt;
  303. #if defined(CONFIG_BOOTP_PXE)
  304. char *uuid;
  305. size_t vci_strlen;
  306. u16 clientarch;
  307. #endif
  308. #if defined(CONFIG_BOOTP_VENDOREX)
  309. u8 *x;
  310. #endif
  311. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  312. char *hostname;
  313. #endif
  314. *e++ = 99; /* RFC1048 Magic Cookie */
  315. *e++ = 130;
  316. *e++ = 83;
  317. *e++ = 99;
  318. *e++ = 53; /* DHCP Message Type */
  319. *e++ = 1;
  320. *e++ = message_type;
  321. *e++ = 57; /* Maximum DHCP Message Size */
  322. *e++ = 2;
  323. *e++ = (576 - 312 + OPT_SIZE) >> 8;
  324. *e++ = (576 - 312 + OPT_SIZE) & 0xff;
  325. if (ServerID) {
  326. int tmp = ntohl(ServerID);
  327. *e++ = 54; /* ServerID */
  328. *e++ = 4;
  329. *e++ = tmp >> 24;
  330. *e++ = tmp >> 16;
  331. *e++ = tmp >> 8;
  332. *e++ = tmp & 0xff;
  333. }
  334. if (RequestedIP) {
  335. int tmp = ntohl(RequestedIP);
  336. *e++ = 50; /* Requested IP */
  337. *e++ = 4;
  338. *e++ = tmp >> 24;
  339. *e++ = tmp >> 16;
  340. *e++ = tmp >> 8;
  341. *e++ = tmp & 0xff;
  342. }
  343. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  344. hostname = getenv("hostname");
  345. if (hostname) {
  346. int hostnamelen = strlen(hostname);
  347. *e++ = 12; /* Hostname */
  348. *e++ = hostnamelen;
  349. memcpy(e, hostname, hostnamelen);
  350. e += hostnamelen;
  351. }
  352. #endif
  353. #if defined(CONFIG_BOOTP_PXE)
  354. clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
  355. *e++ = 93; /* Client System Architecture */
  356. *e++ = 2;
  357. *e++ = (clientarch >> 8) & 0xff;
  358. *e++ = clientarch & 0xff;
  359. *e++ = 94; /* Client Network Interface Identifier */
  360. *e++ = 3;
  361. *e++ = 1; /* type field for UNDI */
  362. *e++ = 0; /* major revision */
  363. *e++ = 0; /* minor revision */
  364. uuid = getenv("pxeuuid");
  365. if (uuid) {
  366. if (uuid_str_valid(uuid)) {
  367. *e++ = 97; /* Client Machine Identifier */
  368. *e++ = 17;
  369. *e++ = 0; /* type 0 - UUID */
  370. uuid_str_to_bin(uuid, e);
  371. e += 16;
  372. } else {
  373. printf("Invalid pxeuuid: %s\n", uuid);
  374. }
  375. }
  376. *e++ = 60; /* Vendor Class Identifier */
  377. vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
  378. *e++ = vci_strlen;
  379. memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
  380. e += vci_strlen;
  381. #endif
  382. #if defined(CONFIG_BOOTP_VENDOREX)
  383. x = dhcp_vendorex_prep(e);
  384. if (x)
  385. return x - start;
  386. #endif
  387. *e++ = 55; /* Parameter Request List */
  388. cnt = e++; /* Pointer to count of requested items */
  389. *cnt = 0;
  390. #if defined(CONFIG_BOOTP_SUBNETMASK)
  391. *e++ = 1; /* Subnet Mask */
  392. *cnt += 1;
  393. #endif
  394. #if defined(CONFIG_BOOTP_TIMEOFFSET)
  395. *e++ = 2;
  396. *cnt += 1;
  397. #endif
  398. #if defined(CONFIG_BOOTP_GATEWAY)
  399. *e++ = 3; /* Router Option */
  400. *cnt += 1;
  401. #endif
  402. #if defined(CONFIG_BOOTP_DNS)
  403. *e++ = 6; /* DNS Server(s) */
  404. *cnt += 1;
  405. #endif
  406. #if defined(CONFIG_BOOTP_HOSTNAME)
  407. *e++ = 12; /* Hostname */
  408. *cnt += 1;
  409. #endif
  410. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  411. *e++ = 13; /* Boot File Size */
  412. *cnt += 1;
  413. #endif
  414. #if defined(CONFIG_BOOTP_BOOTPATH)
  415. *e++ = 17; /* Boot path */
  416. *cnt += 1;
  417. #endif
  418. #if defined(CONFIG_BOOTP_NISDOMAIN)
  419. *e++ = 40; /* NIS Domain name request */
  420. *cnt += 1;
  421. #endif
  422. #if defined(CONFIG_BOOTP_NTPSERVER)
  423. *e++ = 42;
  424. *cnt += 1;
  425. #endif
  426. /* no options, so back up to avoid sending an empty request list */
  427. if (*cnt == 0)
  428. e -= 2;
  429. *e++ = 255; /* End of the list */
  430. /* Pad to minimal length */
  431. #ifdef CONFIG_DHCP_MIN_EXT_LEN
  432. while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
  433. *e++ = 0;
  434. #endif
  435. return e - start;
  436. }
  437. #else
  438. /*
  439. * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
  440. */
  441. static int BootpExtended(u8 *e)
  442. {
  443. u8 *start = e;
  444. *e++ = 99; /* RFC1048 Magic Cookie */
  445. *e++ = 130;
  446. *e++ = 83;
  447. *e++ = 99;
  448. #if defined(CONFIG_CMD_DHCP)
  449. *e++ = 53; /* DHCP Message Type */
  450. *e++ = 1;
  451. *e++ = DHCP_DISCOVER;
  452. *e++ = 57; /* Maximum DHCP Message Size */
  453. *e++ = 2;
  454. *e++ = (576 - 312 + OPT_SIZE) >> 16;
  455. *e++ = (576 - 312 + OPT_SIZE) & 0xff;
  456. #endif
  457. #if defined(CONFIG_BOOTP_SUBNETMASK)
  458. *e++ = 1; /* Subnet mask request */
  459. *e++ = 4;
  460. e += 4;
  461. #endif
  462. #if defined(CONFIG_BOOTP_GATEWAY)
  463. *e++ = 3; /* Default gateway request */
  464. *e++ = 4;
  465. e += 4;
  466. #endif
  467. #if defined(CONFIG_BOOTP_DNS)
  468. *e++ = 6; /* Domain Name Server */
  469. *e++ = 4;
  470. e += 4;
  471. #endif
  472. #if defined(CONFIG_BOOTP_HOSTNAME)
  473. *e++ = 12; /* Host name request */
  474. *e++ = 32;
  475. e += 32;
  476. #endif
  477. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  478. *e++ = 13; /* Boot file size */
  479. *e++ = 2;
  480. e += 2;
  481. #endif
  482. #if defined(CONFIG_BOOTP_BOOTPATH)
  483. *e++ = 17; /* Boot path */
  484. *e++ = 32;
  485. e += 32;
  486. #endif
  487. #if defined(CONFIG_BOOTP_NISDOMAIN)
  488. *e++ = 40; /* NIS Domain name request */
  489. *e++ = 32;
  490. e += 32;
  491. #endif
  492. #if defined(CONFIG_BOOTP_NTPSERVER)
  493. *e++ = 42;
  494. *e++ = 4;
  495. e += 4;
  496. #endif
  497. *e++ = 255; /* End of the list */
  498. return e - start;
  499. }
  500. #endif
  501. void
  502. BootpRequest(void)
  503. {
  504. uchar *pkt, *iphdr;
  505. struct Bootp_t *bp;
  506. int ext_len, pktlen, iplen;
  507. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
  508. #if defined(CONFIG_CMD_DHCP)
  509. dhcp_state = INIT;
  510. #endif
  511. #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
  512. unsigned char bi_enetaddr[6];
  513. int reg;
  514. ulong tst1, tst2, sum, m_mask, m_value = 0;
  515. if (BootpTry == 0) {
  516. /* get our mac */
  517. eth_getenv_enetaddr("ethaddr", bi_enetaddr);
  518. debug("BootpRequest => Our Mac: ");
  519. for (reg = 0; reg < 6; reg++)
  520. debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
  521. /* Mac-Manipulation 2 get seed1 */
  522. tst1 = 0;
  523. tst2 = 0;
  524. for (reg = 2; reg < 6; reg++) {
  525. tst1 = tst1 << 8;
  526. tst1 = tst1 | bi_enetaddr[reg];
  527. }
  528. for (reg = 0; reg < 2; reg++) {
  529. tst2 = tst2 | bi_enetaddr[reg];
  530. tst2 = tst2 << 8;
  531. }
  532. seed1 = tst1^tst2;
  533. /* Mirror seed1*/
  534. m_mask = 0x1;
  535. for (reg = 1; reg <= 32; reg++) {
  536. m_value |= (m_mask & seed1);
  537. seed1 = seed1 >> 1;
  538. m_value = m_value << 1;
  539. }
  540. seed1 = m_value;
  541. seed2 = 0xB78D0945;
  542. }
  543. /* Random Number Generator */
  544. for (reg = 0; reg <= 0; reg++) {
  545. sum = seed1 + seed2;
  546. if (sum < seed1 || sum < seed2)
  547. sum++;
  548. seed2 = seed1;
  549. seed1 = sum;
  550. if (BootpTry <= 2) { /* Start with max 1024 * 1ms */
  551. sum = sum >> (22-BootpTry);
  552. } else { /*After 3rd BOOTP request max 8192 * 1ms */
  553. sum = sum >> 19;
  554. }
  555. }
  556. printf("Random delay: %ld ms...\n", sum);
  557. for (reg = 0; reg < sum; reg++)
  558. udelay(1000); /*Wait 1ms*/
  559. #endif /* CONFIG_BOOTP_RANDOM_DELAY */
  560. printf("BOOTP broadcast %d\n", ++BootpTry);
  561. pkt = NetTxPacket;
  562. memset((void *)pkt, 0, PKTSIZE);
  563. pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
  564. /*
  565. * Next line results in incorrect packet size being transmitted,
  566. * resulting in errors in some DHCP servers, reporting missing bytes.
  567. * Size must be set in packet header after extension length has been
  568. * determined.
  569. * C. Hallinan, DS4.COM, Inc.
  570. */
  571. /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
  572. sizeof (struct Bootp_t)); */
  573. iphdr = pkt; /* We need this later for NetSetIP() */
  574. pkt += IP_HDR_SIZE;
  575. bp = (struct Bootp_t *)pkt;
  576. bp->bp_op = OP_BOOTREQUEST;
  577. bp->bp_htype = HWT_ETHER;
  578. bp->bp_hlen = HWL_ETHER;
  579. bp->bp_hops = 0;
  580. bp->bp_secs = htons(get_timer(0) / 1000);
  581. NetWriteIP(&bp->bp_ciaddr, 0);
  582. NetWriteIP(&bp->bp_yiaddr, 0);
  583. NetWriteIP(&bp->bp_siaddr, 0);
  584. NetWriteIP(&bp->bp_giaddr, 0);
  585. memcpy(bp->bp_chaddr, NetOurEther, 6);
  586. copy_filename(bp->bp_file, BootFile, sizeof(bp->bp_file));
  587. /* Request additional information from the BOOTP/DHCP server */
  588. #if defined(CONFIG_CMD_DHCP)
  589. ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
  590. #else
  591. ext_len = BootpExtended((u8 *)bp->bp_vend);
  592. #endif
  593. /*
  594. * Bootp ID is the lower 4 bytes of our ethernet address
  595. * plus the current time in ms.
  596. */
  597. BootpID = ((ulong)NetOurEther[2] << 24)
  598. | ((ulong)NetOurEther[3] << 16)
  599. | ((ulong)NetOurEther[4] << 8)
  600. | (ulong)NetOurEther[5];
  601. BootpID += get_timer(0);
  602. BootpID = htonl(BootpID);
  603. NetCopyLong(&bp->bp_id, &BootpID);
  604. /*
  605. * Calculate proper packet lengths taking into account the
  606. * variable size of the options field
  607. */
  608. pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
  609. sizeof(bp->bp_vend) + ext_len;
  610. iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
  611. NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
  612. NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
  613. #if defined(CONFIG_CMD_DHCP)
  614. dhcp_state = SELECTING;
  615. NetSetHandler(DhcpHandler);
  616. #else
  617. NetSetHandler(BootpHandler);
  618. #endif
  619. NetSendPacket(NetTxPacket, pktlen);
  620. }
  621. #if defined(CONFIG_CMD_DHCP)
  622. static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
  623. {
  624. uchar *end = popt + BOOTP_HDR_SIZE;
  625. int oplen, size;
  626. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  627. int *to_ptr;
  628. #endif
  629. while (popt < end && *popt != 0xff) {
  630. oplen = *(popt + 1);
  631. switch (*popt) {
  632. case 1:
  633. NetCopyIP(&NetOurSubnetMask, (popt + 2));
  634. break;
  635. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  636. case 2: /* Time offset */
  637. to_ptr = &NetTimeOffset;
  638. NetCopyLong((ulong *)to_ptr, (ulong *)(popt + 2));
  639. NetTimeOffset = ntohl(NetTimeOffset);
  640. break;
  641. #endif
  642. case 3:
  643. NetCopyIP(&NetOurGatewayIP, (popt + 2));
  644. break;
  645. case 6:
  646. NetCopyIP(&NetOurDNSIP, (popt + 2));
  647. #if defined(CONFIG_BOOTP_DNS2)
  648. if (*(popt + 1) > 4)
  649. NetCopyIP(&NetOurDNS2IP, (popt + 2 + 4));
  650. #endif
  651. break;
  652. case 12:
  653. size = truncate_sz("Host Name",
  654. sizeof(NetOurHostName), oplen);
  655. memcpy(&NetOurHostName, popt + 2, size);
  656. NetOurHostName[size] = 0;
  657. break;
  658. case 15: /* Ignore Domain Name Option */
  659. break;
  660. case 17:
  661. size = truncate_sz("Root Path",
  662. sizeof(NetOurRootPath), oplen);
  663. memcpy(&NetOurRootPath, popt + 2, size);
  664. NetOurRootPath[size] = 0;
  665. break;
  666. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  667. case 42: /* NTP server IP */
  668. NetCopyIP(&NetNtpServerIP, (popt + 2));
  669. break;
  670. #endif
  671. case 51:
  672. NetCopyLong(&dhcp_leasetime, (ulong *) (popt + 2));
  673. break;
  674. case 53: /* Ignore Message Type Option */
  675. break;
  676. case 54:
  677. NetCopyIP(&NetDHCPServerIP, (popt + 2));
  678. break;
  679. case 58: /* Ignore Renewal Time Option */
  680. break;
  681. case 59: /* Ignore Rebinding Time Option */
  682. break;
  683. case 66: /* Ignore TFTP server name */
  684. break;
  685. case 67: /* vendor opt bootfile */
  686. /*
  687. * I can't use dhcp_vendorex_proc here because I need
  688. * to write into the bootp packet - even then I had to
  689. * pass the bootp packet pointer into here as the
  690. * second arg
  691. */
  692. size = truncate_sz("Opt Boot File",
  693. sizeof(bp->bp_file),
  694. oplen);
  695. if (bp->bp_file[0] == '\0' && size > 0) {
  696. /*
  697. * only use vendor boot file if we didn't
  698. * receive a boot file in the main non-vendor
  699. * part of the packet - god only knows why
  700. * some vendors chose not to use this perfectly
  701. * good spot to store the boot file (join on
  702. * Tru64 Unix) it seems mind bogglingly crazy
  703. * to me
  704. */
  705. printf("*** WARNING: using vendor "
  706. "optional boot file\n");
  707. memcpy(bp->bp_file, popt + 2, size);
  708. bp->bp_file[size] = '\0';
  709. }
  710. break;
  711. default:
  712. #if defined(CONFIG_BOOTP_VENDOREX)
  713. if (dhcp_vendorex_proc(popt))
  714. break;
  715. #endif
  716. printf("*** Unhandled DHCP Option in OFFER/ACK:"
  717. " %d\n", *popt);
  718. break;
  719. }
  720. popt += oplen + 2; /* Process next option */
  721. }
  722. }
  723. static int DhcpMessageType(unsigned char *popt)
  724. {
  725. if (NetReadLong((ulong *)popt) != htonl(BOOTP_VENDOR_MAGIC))
  726. return -1;
  727. popt += 4;
  728. while (*popt != 0xff) {
  729. if (*popt == 53) /* DHCP Message Type */
  730. return *(popt + 2);
  731. popt += *(popt + 1) + 2; /* Scan through all options */
  732. }
  733. return -1;
  734. }
  735. static void DhcpSendRequestPkt(struct Bootp_t *bp_offer)
  736. {
  737. uchar *pkt, *iphdr;
  738. struct Bootp_t *bp;
  739. int pktlen, iplen, extlen;
  740. IPaddr_t OfferedIP;
  741. debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
  742. pkt = NetTxPacket;
  743. memset((void *)pkt, 0, PKTSIZE);
  744. pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
  745. iphdr = pkt; /* We'll need this later to set proper pkt size */
  746. pkt += IP_HDR_SIZE;
  747. bp = (struct Bootp_t *)pkt;
  748. bp->bp_op = OP_BOOTREQUEST;
  749. bp->bp_htype = HWT_ETHER;
  750. bp->bp_hlen = HWL_ETHER;
  751. bp->bp_hops = 0;
  752. bp->bp_secs = htons(get_timer(0) / 1000);
  753. /* Do not set the client IP, your IP, or server IP yet, since it
  754. * hasn't been ACK'ed by the server yet */
  755. /*
  756. * RFC3046 requires Relay Agents to discard packets with
  757. * nonzero and offered giaddr
  758. */
  759. NetWriteIP(&bp->bp_giaddr, 0);
  760. memcpy(bp->bp_chaddr, NetOurEther, 6);
  761. /*
  762. * ID is the id of the OFFER packet
  763. */
  764. NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
  765. /*
  766. * Copy options from OFFER packet if present
  767. */
  768. /* Copy offered IP into the parameters request list */
  769. NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
  770. extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST,
  771. NetDHCPServerIP, OfferedIP);
  772. pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE -
  773. sizeof(bp->bp_vend) + extlen;
  774. iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
  775. NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
  776. debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
  777. #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
  778. udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
  779. #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
  780. NetSendPacket(NetTxPacket, pktlen);
  781. }
  782. /*
  783. * Handle DHCP received packets.
  784. */
  785. static void
  786. DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  787. unsigned len)
  788. {
  789. struct Bootp_t *bp = (struct Bootp_t *)pkt;
  790. debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
  791. src, dest, len, dhcp_state);
  792. /* Filter out pkts we don't want */
  793. if (BootpCheckPkt(pkt, dest, src, len))
  794. return;
  795. debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state:"
  796. " %d\n", src, dest, len, dhcp_state);
  797. switch (dhcp_state) {
  798. case SELECTING:
  799. /*
  800. * Wait an appropriate time for any potential DHCPOFFER packets
  801. * to arrive. Then select one, and generate DHCPREQUEST
  802. * response. If filename is in format we recognize, assume it
  803. * is a valid OFFER from a server we want.
  804. */
  805. debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
  806. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  807. if (strncmp(bp->bp_file,
  808. CONFIG_SYS_BOOTFILE_PREFIX,
  809. strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
  810. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  811. debug("TRANSITIONING TO REQUESTING STATE\n");
  812. dhcp_state = REQUESTING;
  813. if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
  814. htonl(BOOTP_VENDOR_MAGIC))
  815. DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
  816. NetSetTimeout(TIMEOUT, BootpTimeout);
  817. DhcpSendRequestPkt(bp);
  818. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  819. }
  820. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  821. return;
  822. break;
  823. case REQUESTING:
  824. debug("DHCP State: REQUESTING\n");
  825. if (DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK) {
  826. if (NetReadLong((ulong *)&bp->bp_vend[0]) ==
  827. htonl(BOOTP_VENDOR_MAGIC))
  828. DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
  829. /* Store net params from reply */
  830. BootpCopyNetParams(bp);
  831. dhcp_state = BOUND;
  832. printf("DHCP client bound to address %pI4\n",
  833. &NetOurIP);
  834. bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
  835. "bootp_stop");
  836. net_auto_load();
  837. return;
  838. }
  839. break;
  840. case BOUND:
  841. /* DHCP client bound to address */
  842. break;
  843. default:
  844. puts("DHCP: INVALID STATE\n");
  845. break;
  846. }
  847. }
  848. void DhcpRequest(void)
  849. {
  850. BootpRequest();
  851. }
  852. #endif /* CONFIG_CMD_DHCP */