bootp.c 22 KB

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