bootp.c 23 KB

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