bootp.c 23 KB

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