bootp.c 23 KB

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