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