bootp.c 23 KB

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