bootp.c 22 KB

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