bootp.c 22 KB

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