bootp.c 23 KB

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