bootp.c 21 KB

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