bootp.c 23 KB

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