bootp.c 21 KB

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