bootp.c 21 KB

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