bootp.c 23 KB

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