bootp.c 22 KB

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