bootp.c 22 KB

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