bootp.c 22 KB

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