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