bootp.c 22 KB

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