bootp.c 23 KB

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