bootp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 5000UL /* Milliseconds 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. printf ("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);
  244. if (NetOurGatewayIP)
  245. printf ("NetOurGatewayIP : %pI4", &NetOurGatewayIP);
  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=%zu)\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((uchar *)&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 defined(CONFIG_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
  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, BootpTimeout);
  320. BootpRequest ();
  321. }
  322. }
  323. /*
  324. * Initialize BOOTP extension fields in the request.
  325. */
  326. #if defined(CONFIG_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 defined(CONFIG_BOOTP_VENDOREX)
  332. u8 *x;
  333. #endif
  334. #if defined(CONFIG_BOOTP_SEND_HOSTNAME)
  335. char *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 defined(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 defined(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 defined(CONFIG_BOOTP_SUBNETMASK)
  383. *e++ = 1; /* Subnet Mask */
  384. *cnt += 1;
  385. #endif
  386. #if defined(CONFIG_BOOTP_TIMEOFFSET)
  387. *e++ = 2;
  388. *cnt += 1;
  389. #endif
  390. #if defined(CONFIG_BOOTP_GATEWAY)
  391. *e++ = 3; /* Router Option */
  392. *cnt += 1;
  393. #endif
  394. #if defined(CONFIG_BOOTP_DNS)
  395. *e++ = 6; /* DNS Server(s) */
  396. *cnt += 1;
  397. #endif
  398. #if defined(CONFIG_BOOTP_HOSTNAME)
  399. *e++ = 12; /* Hostname */
  400. *cnt += 1;
  401. #endif
  402. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  403. *e++ = 13; /* Boot File Size */
  404. *cnt += 1;
  405. #endif
  406. #if defined(CONFIG_BOOTP_BOOTPATH)
  407. *e++ = 17; /* Boot path */
  408. *cnt += 1;
  409. #endif
  410. #if defined(CONFIG_BOOTP_NISDOMAIN)
  411. *e++ = 40; /* NIS Domain name request */
  412. *cnt += 1;
  413. #endif
  414. #if defined(CONFIG_BOOTP_NTPSERVER)
  415. *e++ = 42;
  416. *cnt += 1;
  417. #endif
  418. *e++ = 255; /* End of the list */
  419. /* Pad to minimal length */
  420. #ifdef CONFIG_DHCP_MIN_EXT_LEN
  421. while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN)
  422. *e++ = 0;
  423. #endif
  424. return e - start;
  425. }
  426. #else
  427. /*
  428. * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
  429. */
  430. static int BootpExtended (u8 * e)
  431. {
  432. u8 *start = e;
  433. *e++ = 99; /* RFC1048 Magic Cookie */
  434. *e++ = 130;
  435. *e++ = 83;
  436. *e++ = 99;
  437. #if defined(CONFIG_CMD_DHCP)
  438. *e++ = 53; /* DHCP Message Type */
  439. *e++ = 1;
  440. *e++ = DHCP_DISCOVER;
  441. *e++ = 57; /* Maximum DHCP Message Size */
  442. *e++ = 2;
  443. *e++ = (576 - 312 + OPT_SIZE) >> 16;
  444. *e++ = (576 - 312 + OPT_SIZE) & 0xff;
  445. #endif
  446. #if defined(CONFIG_BOOTP_SUBNETMASK)
  447. *e++ = 1; /* Subnet mask request */
  448. *e++ = 4;
  449. e += 4;
  450. #endif
  451. #if defined(CONFIG_BOOTP_GATEWAY)
  452. *e++ = 3; /* Default gateway request */
  453. *e++ = 4;
  454. e += 4;
  455. #endif
  456. #if defined(CONFIG_BOOTP_DNS)
  457. *e++ = 6; /* Domain Name Server */
  458. *e++ = 4;
  459. e += 4;
  460. #endif
  461. #if defined(CONFIG_BOOTP_HOSTNAME)
  462. *e++ = 12; /* Host name request */
  463. *e++ = 32;
  464. e += 32;
  465. #endif
  466. #if defined(CONFIG_BOOTP_BOOTFILESIZE)
  467. *e++ = 13; /* Boot file size */
  468. *e++ = 2;
  469. e += 2;
  470. #endif
  471. #if defined(CONFIG_BOOTP_BOOTPATH)
  472. *e++ = 17; /* Boot path */
  473. *e++ = 32;
  474. e += 32;
  475. #endif
  476. #if defined(CONFIG_BOOTP_NISDOMAIN)
  477. *e++ = 40; /* NIS Domain name request */
  478. *e++ = 32;
  479. e += 32;
  480. #endif
  481. *e++ = 255; /* End of the list */
  482. return e - start;
  483. }
  484. #endif
  485. void
  486. BootpRequest (void)
  487. {
  488. volatile uchar *pkt, *iphdr;
  489. Bootp_t *bp;
  490. int ext_len, pktlen, iplen;
  491. #if defined(CONFIG_CMD_DHCP)
  492. dhcp_state = INIT;
  493. #endif
  494. #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
  495. unsigned char bi_enetaddr[6];
  496. int reg;
  497. char *e,*s;
  498. char tmp[64];
  499. ulong tst1, tst2, sum, m_mask, m_value = 0;
  500. if (BootpTry ==0) {
  501. /* get our mac */
  502. reg = getenv_r ("ethaddr", tmp, sizeof(tmp));
  503. s = (reg > 0) ? tmp : NULL;
  504. for (reg=0; reg<6; ++reg) {
  505. bi_enetaddr[reg] = s ? simple_strtoul(s, &e, 16) : 0;
  506. if (s) {
  507. s = (*e) ? e+1 : e;
  508. }
  509. }
  510. #ifdef DEBUG
  511. puts ("BootpRequest => Our Mac: ");
  512. for (reg=0; reg<6; reg++) {
  513. printf ("%x%c",
  514. bi_enetaddr[reg],
  515. reg==5 ? '\n' : ':');
  516. }
  517. #endif /* DEBUG */
  518. /* Mac-Manipulation 2 get seed1 */
  519. tst1=0;
  520. tst2=0;
  521. for (reg=2; reg<6; reg++) {
  522. tst1 = tst1 << 8;
  523. tst1 = tst1 | bi_enetaddr[reg];
  524. }
  525. for (reg=0; reg<2; reg++) {
  526. tst2 = tst2 | bi_enetaddr[reg];
  527. tst2 = tst2 << 8;
  528. }
  529. seed1 = tst1^tst2;
  530. /* Mirror seed1*/
  531. m_mask=0x1;
  532. for (reg=1;reg<=32;reg++) {
  533. m_value |= (m_mask & seed1);
  534. seed1 = seed1 >> 1;
  535. m_value = m_value << 1;
  536. }
  537. seed1 = m_value;
  538. seed2 = 0xB78D0945;
  539. }
  540. /* Random Number Generator */
  541. for (reg=0;reg<=0;reg++) {
  542. sum = seed1 + seed2;
  543. if (sum < seed1 || sum < seed2)
  544. sum++;
  545. seed2 = seed1;
  546. seed1 = sum;
  547. if (BootpTry<=2) { /* Start with max 1024 * 1ms */
  548. sum = sum >> (22-BootpTry);
  549. } else { /*After 3rd BOOTP request max 8192 * 1ms */
  550. sum = sum >> 19;
  551. }
  552. }
  553. printf ("Random delay: %ld ms...\n", sum);
  554. for (reg=0; reg <sum; reg++) {
  555. udelay(1000); /*Wait 1ms*/
  556. }
  557. #endif /* CONFIG_BOOTP_RANDOM_DELAY */
  558. printf("BOOTP broadcast %d\n", ++BootpTry);
  559. pkt = NetTxPacket;
  560. memset ((void*)pkt, 0, PKTSIZE);
  561. pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
  562. /*
  563. * Next line results in incorrect packet size being transmitted, resulting
  564. * in errors in some DHCP servers, reporting missing bytes. Size must be
  565. * set in packet header after extension length has been determined.
  566. * C. Hallinan, DS4.COM, Inc.
  567. */
  568. /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */
  569. iphdr = pkt; /* We need this later for NetSetIP() */
  570. pkt += IP_HDR_SIZE;
  571. bp = (Bootp_t *)pkt;
  572. bp->bp_op = OP_BOOTREQUEST;
  573. bp->bp_htype = HWT_ETHER;
  574. bp->bp_hlen = HWL_ETHER;
  575. bp->bp_hops = 0;
  576. bp->bp_secs = htons(get_timer(0) / 1000);
  577. NetWriteIP(&bp->bp_ciaddr, 0);
  578. NetWriteIP(&bp->bp_yiaddr, 0);
  579. NetWriteIP(&bp->bp_siaddr, 0);
  580. NetWriteIP(&bp->bp_giaddr, 0);
  581. memcpy (bp->bp_chaddr, NetOurEther, 6);
  582. copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
  583. /* Request additional information from the BOOTP/DHCP server */
  584. #if defined(CONFIG_CMD_DHCP)
  585. ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
  586. #else
  587. ext_len = BootpExtended((u8 *)bp->bp_vend);
  588. #endif
  589. /*
  590. * Bootp ID is the lower 4 bytes of our ethernet address
  591. * plus the current time in ms.
  592. */
  593. BootpID = ((ulong)NetOurEther[2] << 24)
  594. | ((ulong)NetOurEther[3] << 16)
  595. | ((ulong)NetOurEther[4] << 8)
  596. | (ulong)NetOurEther[5];
  597. BootpID += get_timer(0);
  598. BootpID = htonl(BootpID);
  599. NetCopyLong(&bp->bp_id, &BootpID);
  600. /*
  601. * Calculate proper packet lengths taking into account the
  602. * variable size of the options field
  603. */
  604. pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + ext_len;
  605. iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len;
  606. NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
  607. NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
  608. #if defined(CONFIG_CMD_DHCP)
  609. dhcp_state = SELECTING;
  610. NetSetHandler(DhcpHandler);
  611. #else
  612. NetSetHandler(BootpHandler);
  613. #endif
  614. NetSendPacket(NetTxPacket, pktlen);
  615. }
  616. #if defined(CONFIG_CMD_DHCP)
  617. static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
  618. {
  619. uchar *end = popt + BOOTP_HDR_SIZE;
  620. int oplen, size;
  621. while (popt < end && *popt != 0xff) {
  622. oplen = *(popt + 1);
  623. switch (*popt) {
  624. case 1:
  625. NetCopyIP (&NetOurSubnetMask, (popt + 2));
  626. break;
  627. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  628. case 2: /* Time offset */
  629. NetCopyLong ((ulong *)&NetTimeOffset, (ulong *) (popt + 2));
  630. NetTimeOffset = ntohl (NetTimeOffset);
  631. break;
  632. #endif
  633. case 3:
  634. NetCopyIP (&NetOurGatewayIP, (popt + 2));
  635. break;
  636. case 6:
  637. NetCopyIP (&NetOurDNSIP, (popt + 2));
  638. #if defined(CONFIG_BOOTP_DNS2)
  639. if (*(popt + 1) > 4) {
  640. NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
  641. }
  642. #endif
  643. break;
  644. case 12:
  645. size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen);
  646. memcpy (&NetOurHostName, popt + 2, size);
  647. NetOurHostName[size] = 0;
  648. break;
  649. case 15: /* Ignore Domain Name Option */
  650. break;
  651. case 17:
  652. size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen);
  653. memcpy (&NetOurRootPath, popt + 2, size);
  654. NetOurRootPath[size] = 0;
  655. break;
  656. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  657. case 42: /* NTP server IP */
  658. NetCopyIP (&NetNtpServerIP, (popt + 2));
  659. break;
  660. #endif
  661. case 51:
  662. NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
  663. break;
  664. case 53: /* Ignore Message Type Option */
  665. break;
  666. case 54:
  667. NetCopyIP (&NetDHCPServerIP, (popt + 2));
  668. break;
  669. case 58: /* Ignore Renewal Time Option */
  670. break;
  671. case 59: /* Ignore Rebinding Time Option */
  672. break;
  673. case 66: /* Ignore TFTP server name */
  674. break;
  675. case 67: /* vendor opt bootfile */
  676. /*
  677. * I can't use dhcp_vendorex_proc here because I need
  678. * to write into the bootp packet - even then I had to
  679. * pass the bootp packet pointer into here as the
  680. * second arg
  681. */
  682. size = truncate_sz ("Opt Boot File",
  683. sizeof(bp->bp_file),
  684. oplen);
  685. if (bp->bp_file[0] == '\0' && size > 0) {
  686. /*
  687. * only use vendor boot file if we didn't
  688. * receive a boot file in the main non-vendor
  689. * part of the packet - god only knows why
  690. * some vendors chose not to use this perfectly
  691. * good spot to store the boot file (join on
  692. * Tru64 Unix) it seems mind bogglingly crazy
  693. * to me
  694. */
  695. printf("*** WARNING: using vendor "
  696. "optional boot file\n");
  697. memcpy(bp->bp_file, popt + 2, size);
  698. bp->bp_file[size] = '\0';
  699. }
  700. break;
  701. default:
  702. #if defined(CONFIG_BOOTP_VENDOREX)
  703. if (dhcp_vendorex_proc (popt))
  704. break;
  705. #endif
  706. printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt);
  707. break;
  708. }
  709. popt += oplen + 2; /* Process next option */
  710. }
  711. }
  712. static int DhcpMessageType(unsigned char *popt)
  713. {
  714. if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC))
  715. return -1;
  716. popt += 4;
  717. while ( *popt != 0xff ) {
  718. if ( *popt == 53 ) /* DHCP Message Type */
  719. return *(popt + 2);
  720. popt += *(popt + 1) + 2; /* Scan through all options */
  721. }
  722. return -1;
  723. }
  724. static void DhcpSendRequestPkt(Bootp_t *bp_offer)
  725. {
  726. volatile uchar *pkt, *iphdr;
  727. Bootp_t *bp;
  728. int pktlen, iplen, extlen;
  729. IPaddr_t OfferedIP;
  730. debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n");
  731. pkt = NetTxPacket;
  732. memset ((void*)pkt, 0, PKTSIZE);
  733. pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
  734. iphdr = pkt; /* We'll need this later to set proper pkt size */
  735. pkt += IP_HDR_SIZE;
  736. bp = (Bootp_t *)pkt;
  737. bp->bp_op = OP_BOOTREQUEST;
  738. bp->bp_htype = HWT_ETHER;
  739. bp->bp_hlen = HWL_ETHER;
  740. bp->bp_hops = 0;
  741. bp->bp_secs = htons(get_timer(0) / 1000);
  742. /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by
  743. * the server yet */
  744. /*
  745. * RFC3046 requires Relay Agents to discard packets with
  746. * nonzero and offered giaddr
  747. */
  748. NetWriteIP(&bp->bp_giaddr, 0);
  749. memcpy (bp->bp_chaddr, NetOurEther, 6);
  750. /*
  751. * ID is the id of the OFFER packet
  752. */
  753. NetCopyLong(&bp->bp_id, &bp_offer->bp_id);
  754. /*
  755. * Copy options from OFFER packet if present
  756. */
  757. /* Copy offered IP into the parameters request list */
  758. NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr);
  759. extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
  760. pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen;
  761. iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
  762. NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
  763. debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
  764. #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
  765. udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
  766. #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
  767. NetSendPacket(NetTxPacket, pktlen);
  768. }
  769. /*
  770. * Handle DHCP received packets.
  771. */
  772. static void
  773. DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
  774. {
  775. Bootp_t *bp = (Bootp_t *)pkt;
  776. debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
  777. src, dest, len, dhcp_state);
  778. if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */
  779. return;
  780. debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n",
  781. src, dest, len, dhcp_state);
  782. switch (dhcp_state) {
  783. case SELECTING:
  784. /*
  785. * Wait an appropriate time for any potential DHCPOFFER packets
  786. * to arrive. Then select one, and generate DHCPREQUEST response.
  787. * If filename is in format we recognize, assume it is a valid
  788. * OFFER from a server we want.
  789. */
  790. debug ("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
  791. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  792. if (strncmp(bp->bp_file,
  793. CONFIG_SYS_BOOTFILE_PREFIX,
  794. strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) {
  795. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  796. debug ("TRANSITIONING TO REQUESTING STATE\n");
  797. dhcp_state = REQUESTING;
  798. if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
  799. DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
  800. NetSetTimeout(TIMEOUT, BootpTimeout);
  801. DhcpSendRequestPkt(bp);
  802. #ifdef CONFIG_SYS_BOOTFILE_PREFIX
  803. }
  804. #endif /* CONFIG_SYS_BOOTFILE_PREFIX */
  805. return;
  806. break;
  807. case REQUESTING:
  808. debug ("DHCP State: REQUESTING\n");
  809. if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
  810. char *s;
  811. if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
  812. DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
  813. BootpCopyNetParams(bp); /* Store net params from reply */
  814. dhcp_state = BOUND;
  815. printf ("DHCP client bound to address %pI4\n", &NetOurIP);
  816. /* Obey the 'autoload' setting */
  817. if ((s = getenv("autoload")) != NULL) {
  818. if (*s == 'n') {
  819. /*
  820. * Just use BOOTP to configure system;
  821. * Do not use TFTP to load the bootfile.
  822. */
  823. NetState = NETLOOP_SUCCESS;
  824. return;
  825. #if defined(CONFIG_CMD_NFS)
  826. } else if (strcmp(s, "NFS") == 0) {
  827. /*
  828. * Use NFS to load the bootfile.
  829. */
  830. NfsStart();
  831. return;
  832. #endif
  833. }
  834. }
  835. TftpStart();
  836. return;
  837. }
  838. break;
  839. case BOUND:
  840. /* DHCP client bound to address */
  841. break;
  842. default:
  843. puts ("DHCP: INVALID STATE\n");
  844. break;
  845. }
  846. }
  847. void DhcpRequest(void)
  848. {
  849. BootpRequest();
  850. }
  851. #endif /* CONFIG_CMD_DHCP */
  852. #endif /* CONFIG_CMD_NET */